12

Is there a way to get the Android ID for a menu item? I can see getTitle() but not a getId(). I'm interested in the ID value "menu_printer_settings" rather than the title value "printer_settings" and the menu item ID (getItemId()). I need this ID to make my Monkey Talk scripts work for localized builds also.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/menu_printer_settings"        
    android:title="@string/printer_settings"
/>

AndroidDev
  • 5,193
  • 5
  • 37
  • 68

5 Answers5

13

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

SteJav
  • 495
  • 4
  • 11
12

Try this:

public boolean onOptionsItemSelected(MenuItem item) {

            switch (item.getItemId()) {

            case R.id.menu_printer_settings:
                    //do what you want  
                    break;
                }
}
Jarvis
  • 1,527
  • 12
  • 17
  • Can I get the ID string name "menu_printer_settings" programmatically? – AndroidDev Jun 05 '13 at 12:18
  • it is simple: item.getTitle() – Jarvis Jun 06 '13 at 10:08
  • the title and the ID string are different. The title is "printer_settings" but I'm interested in the ID string "menu_printer_settings" which is defined in the XML file. – AndroidDev Jun 06 '13 at 12:45
  • the reason why we need the ID is that our scripts (MonkeyTalk automation tool) can run on all the localized builds if we just record once using the ID. If we use the title then the scripts wouldn't playback on a different language. – AndroidDev Jun 07 '13 at 14:21
0

Try using getItemID() also have a look at the following if you are still having problems:

MenuItem.getItemId returns 0 instead of ItemId

Community
  • 1
  • 1
Wattcey
  • 113
  • 1
  • 7
0
Menu menu = null;
menu.findItem(R.id.item_id);

This way you access the menu item anyway in activity or fragment

David Buck
  • 3,752
  • 35
  • 31
  • 35
-1

Solved it by getting all the fields for the package

        Map<Integer, String> idMap = new HashMap<Integer, String>();
        Class<?> r;
        String rClass = activity.getBaseContext().getApplicationContext().getPackageName()
                + ".R$id";
        try {
            r = Class.forName(rClass);
        } catch (ClassNotFoundException e1) {
            Log.log("Unable to load " + rClass + ": " + e1.getMessage());
            return idMap;
        }
        for (Field f : r.getFields()) {
            int val;
            try {
                val = f.getInt(null);
            } catch (Exception e) {
                throw new IllegalStateException("Unable to get value for " + f.getName() + ": "
                        + e.getMessage());
            }
            idMap.put(val, f.getName());

        }
AndroidDev
  • 5,193
  • 5
  • 37
  • 68