I am currently trying to add a sub menu and get it to do what I want. I’ve been looking online and can’t seem to see if it’s possible.
Right basically I have a button called ‘sign-in’ which the users will press to lad the submenu in which will have a label saying Username and then an editable text box allowing the user to type in their desired user name.
Is this possible?
The code I currently have is
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
TextView txtUsername = (TextView) findViewById(R.id.txtUsername);
EditText edit_username = (EditText) findViewById(R.id.edit_username);
super.onCreateOptionsMenu(menu);
SubMenu sub = menu.addSubMenu(0,1,0, "Sign-In");
sub.add(0,11,0,edit_username); //I have noticed this is wrong but the only way I thought it would work
sub.add(0,12,0,txtUsername); //same for here
//add button for okaying the username
return true;
}
obviously this is in the create options menu but like I said I would like it to load the menu when the user clicks sign in using the signIn void
public void signIn(View view)
{
//load menu here
}
You can add any number sub menus to the menu items but you cannot nest a sub menu. (Reference)
Menus with sub menus are used in rare cases where there are a number of related sub menu items for a menu item.
Refer this document to learn more about options menu.
I don’t see a point in adding the user name and password fields to an options menu. You could use a sub activity or a dialog fragment to display those fields.
In your code, you have two problems.
add (int groupId, int itemId, int order, CharSequence title)obviously takes CharSequence as parameter not the View widgets (your edittext and textview).