PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 8, 2022

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

 November 08, 2022     android, menu, monkeyrunner, resources, string     No comments   

Issue

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"
/>


Solution

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());

        }


Answered By - AndroidDev
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing