16

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="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/categoryEditButton"
          android:title="@string/edit"
          android:icon="@drawable/edit" />
    <item android:id="@+id/categoryMoveUpButton"
          android:title="@string/move_up"
          android:icon="@drawable/up" />
    <item android:id="@+id/categoryMoveDownButton"
          android:title="@string/move_down"
          android:icon="@drawable/down" />
    <item android:id="@+id/categoryDeleteButton"
          android:title="@string/delete"
          android:icon="@drawable/trash" />
</menu>

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(R.menu.some_menu, menu);

...

Can anybody help me with this? Thanks.

Dmytro Zarezenko
  • 10,526
  • 11
  • 62
  • 104

4 Answers4

26

This will help:

...

PopupMenu p  = new PopupMenu(this, null);
Menu menu = p.getMenu();
getMenuInflater().inflate(R.menu.some_menu, 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.

Raul
  • 261
  • 3
  • 3
  • This should be the right answer. And it works for API 7+ using Support library v4 – Iree Sep 23 '15 at 16:03
  • @Iree, There is no right answer to this question, as it's not supported in Android. This is just workaround – Farid Aug 23 '20 at 14:01
1

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

private Menu newMenuInstance(Context context) {
    try {
        Class<?> menuBuilderClass = Class.forName("com.android.internal.view.menu.MenuBuilder");
        Constructor<?> constructor = menuBuilderClass.getDeclaredConstructor(Context.class);
        return (Menu) constructor.newInstance(context);
    } catch (Exception e){
        MyLog.GetMyLog().e(e);
    }
    return null;
}
Community
  • 1
  • 1
1

Simple solution: https://gist.github.com/SaifurRahmanMohsin/8c9df2838d9a52830eb9

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

and

MenuItem item = menu.getItem(position)
-1

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.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • No, you don't understand me. I want create my own Menu and want write method like this one: public void setItemsFromResource(int menuResourceId) {...} for parsing menu from standard menu resource XML. I wrote this functionality via my own XML format, but I want parse standard XML menu resource format :) Get drawable objects from it for icons, get strings from resource, etc. – Dmytro Zarezenko Jan 05 '12 at 22:22
  • Check source code of how android does it ... https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/MenuInflater.java – Debjit Jul 18 '18 at 13:35