1

How do I detect keypress and which key user pressed on SubMenu? [the one on the actionbar where user press and a long list would drop down]

 SubMenu subMenu1 = menu.addSubMenu("Option");
        subMenu1.add("Comments");
        subMenu1.add("More screens");
        subMenu1.add("Copy Website URL");
        subMenu1.add("Go to Website");

        MenuItem subMenu1Item = subMenu1.getItem();
        subMenu1Item.setIcon(R.drawable.icon_share);
        subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        finish();
        break;
    }

    return true;
}
MrYanDao
  • 1,253
  • 1
  • 15
  • 27

1 Answers1

2

I suppose you mean "which item a user selects" in the menu, and not "which key a user pressed". You can provide the action in the onOptionsItemSelected() method that you already have. But before, you have to...

Finally, just use the itemId parameter from the add() method (first solution) or the android:id from the XML (second solution) to decide on an action in the switch statement of onOptionsItemSelected().

Community
  • 1
  • 1
saschoar
  • 8,150
  • 5
  • 43
  • 46
  • Can u help me in changing the colour of the submenu items @saschoar? I searched the code of ABS and in abs_styles.xml, in – Diffy Jun 14 '14 at 21:01