Parsing Android menu XML resource to objects list

I can't resolve this problem for 3 days. I have simple XML resource for menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="">
    <item android:id="@+id/categoryEditButton"
          android:icon="@drawable/edit" />
    <item android:id="@+id/categoryMoveUpButton"
          android:icon="@drawable/up" />
    <item android:id="@+id/categoryMoveDownButton"
          android:icon="@drawable/down" />
    <item android:id="@+id/categoryDeleteButton"
          android:icon="@drawable/trash" />

I want to receive List<MenuItem> after parsing of this XML:

public class MenuItem { 
    private CharSequence text;
    private Drawable image;
    private int actionTag;

    //... getters and setters ...

I need this for non-standard manipulations with MenuItems and can't work with this Resourse with standard methods like:


MenuInflater inflater = getMenuInflater();
inflater.inflate(, menu);


Can anybody help me with this? Thanks.


You can easily change menu items at runtime.

Update after comment:

  1. Parse XML to get resource IDs and resource types (images, strings, drawables, etc..)

  2. Access resources via Resources class. Every resource type has different method to access it.

This will help:


PopupMenu p  = new PopupMenu(this, null);
Menu menu = p.getMenu();
getMenuInflater().inflate(, menu);

//Usage of menu
System.out.println("LOG id: "+ menu.getItem(0).getItemId());
System.out.println("LOG title: "+ menu.getItem(0).getTitle());
System.out.println("LOG icon: "+ menu.getItem(0).getIcon());


The creation of a PopupMenu its just a trick to create a Menu object that when inflated will be filled with the information defined on your xml.

Thanks Raul. It don't work for 2.33. I have found solution Here.

private Menu newMenuInstance(Context context) {
    try {
        Class<?> menuBuilderClass = Class.forName("");
        Constructor<?> constructor = menuBuilderClass.getDeclaredConstructor(Context.class);
        return (Menu) constructor.newInstance(context);
    } catch (Exception e){
    return null;

Simple solution:

Menu menu = new MenuBuilder(context);
new MenuInflater(context).inflate(, menu);


MenuItem item = menu.getItem(position)

Need Your Help

C# Converting List<int> to List<double>

c# generics

I have a List&lt;int&gt; and I want to convert it to a List&lt;double&gt;. Is there any way to do this other than just looping through the List&lt;int&gt; and adding to a new List&lt;double&gt; lik...


sql oracle ddl

Which are the differences between DBMS_UTILITY.EXEC_DDL_STATEMENT and EXECUTE IMMEDIATE?