How to get the Android ID for a menu item in Android?

Solution1:

MenuItem item

String[] id = getResources().getResourceName(item.getItemId()).split("\\/");

then access id[1]

Solution2:

Use titleCondensed to match the id e.g.

<menu>
            <item android:id="@+id/myid"
                  android:title="some menu title"
                  android:titleCondensed="myid"/>
...
</menu>

then

String selectedMenuIdString = (String) item.getTitleCondensed();

I prefer Solution 1 since I don't have to repeat the id name.

Hope this helps. Regards Steve


Try this:

public boolean onOptionsItemSelected(MenuItem item) {

            switch (item.getItemId()) {

            case R.id.menu_printer_settings:
                    //do what you want  
                    break;
                }
}