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