2

I am making an android app and I am trying to apply some java code to the xml menu items but they are not getting inflated. I have some menu items defined within my menu xml file (stored in res/menu/main.xml) defined as follows:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".HomeActivity"
    app:showAsAction="always">

    <item android:id="@+id/action_close"
          android:title="Close"
          android:icon="@drawable/exitediting"
          android:enabled="false"
          app:showAsAction="always"/>

    <item android:id="@+id/action_edit"
          android:title="Edit"
          android:icon="@drawable/editbutton"
          android:enabled="false"
          app:showAsAction="always"/>

    <item android:id="@+id/action_mainedit"
          android:title="MainEdit"
          android:icon="@drawable/exitediting"
          android:enabled="true"
          app:showAsAction="always"/>

</menu>

Within my main activity java code I then override the menu inflator as follows:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    createAnimatorSets();
    return true;
}

Then within my createAnimatorSets function I try and access the buttons like this

final ActionMenuItemView editButton = (ActionMenuItemView) toolbar.getChildAt(1).findViewById(R.id.action_edit);
final ActionMenuItemView closeButton = (ActionMenuItemView) toolbar.getChildAt(1).findViewById(R.id.action_close);
final ActionMenuItemView mainButton = (ActionMenuItemView) toolbar.getChildAt(1).findViewById(R.id.action_mainedit);

This simply returns null. I tried to put a break point at this point in the code and watch a few variables. R.id.action_edit is returning the id as expected but both findViewById(R.id.action_edit) and toolbar.getChildAt(1).findViewById(R.id.action_edit) are returning null. The toolbar itself (variable name toolbar) is there and the buttons are stored inside but they are for some reason not inflated in order to be utilised. I have also tried to do it in the onStart method rather than onCreate but this does not work either. Does anyone know how to fix this.

Thanks

kabeersvohra
  • 1,049
  • 1
  • 14
  • 31

2 Answers2

0

You can get a reference to your MenuItems in the onCreateOptionsMenu(Menu menu) method by using

MenuItem edit = menu.findItem(R.id.action_edit);
View actionViewEdit = edit.getActionView();

once you have inflated your menu.

Frederik Schweiger
  • 8,472
  • 6
  • 25
  • 27
  • Thank you for your input. I have tried this and the menu.findItem actually returns a menuitemimpl object but then the getactionview method still returns null – kabeersvohra Mar 22 '15 at 22:28
0

So after a lot of testing it seems that the menu only gets inflated in the onResume. This led to a very hacky solution to my problem but in the future if people struggle with this you need to do your stuff in onresume and just set the buttons to hidden initially within the xml if needed as I have done

kabeersvohra
  • 1,049
  • 1
  • 14
  • 31