13

I have this item in my options menu:

<item
    android:id="@+id/opt_mnu_action"
    android:icon="@android:drawable/ic_dialog_info"
    android:orderInCategory="1"
    android:showAsAction="ifRoom"
    android:title="New">
</item>

The menu itself created in main FragmentActivity. I want to change this item's icon programmatically depending on the open Fragment and, obviously, have different actions when the user hits this button. I tried several things to do that, but nothing worked. The last thing I tried was this code in my Fragment's onCreateView method:

MenuItem mi = (MenuItem) view.findViewById(R.id.opt_mnu_action);
mi.setIcon(R.drawable.ico_1);

But my app crashed. So is there a way to do that?

**UPDATE**

Here's what I'm trying to do now, all in my main main FragmentActivity: First of all I have a MenuItem action_button; in my hierarchy view. Then in my onCreateOptionsMenu method I instantiate it:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    action_button = menu.findItem(R.id.opt_mnu_action);
    return super.onCreateOptionsMenu(menu);
}

Then I created this function to change the icon according to the open tab:

public void change_action_button_icon(int tab_position)
{
    switch(tab_position)
    {
    case 0:
        action_button.setIcon(R.drawable.ico_1);
        break;
    case 1:
        action_button.setIcon(R.drawable.ico_2);
        break;
    case 2:
        action_button.setIcon(R.drawable.ico_3);
        break;
    }
    invalidateOptionsMenu();
}

And I call it in my onTabSelected method:

public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    mViewPager.setCurrentItem(tab.getPosition());
    setTab_position(tab.getPosition());
    change_action_button_icon(tab.getPosition());
}

But once I start my app - it crashes. I get NullPointerException error at this line:

action_button.setIcon(R.drawable.ico_1);

My guess - it happens because the icon change was requested before the action_button was instantiated. But I don't know how to overcome it...

3 Answers 3

19

Use this to get a reference to the menu item:

    menu.findItem(resourceId).setIcon(drawableId);

You have to put the code to change the icon in onCreateOptionsMenu().

Please refer to my example below:

    public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    getMenuInflater().inflate(R.menu.option_menu, menu);

    if (needToChangeMenuItem){

        menu.findItem(resourceId).setIcon(drawableId);
    }

    manageMenuIcon(menu);

    needToChangeMenuItem = false;


    return true;
}


    public void manageMenuIcon(Menu menu){
    if (bluetoothIconOn){
        menu.findItem(R.id.secure_connect_scan).setIcon(R.drawable.bluetoothon);
    } else
        menu.findItem(R.id.secure_connect_scan).setIcon(R.drawable.bluetoothoff);

    if (gpsIconOn)
        menu.findItem(R.id.gps).setIcon(R.drawable.gps);
    else
        menu.findItem(R.id.gps).setVisible(false);

    if (slipAndDropIconOn)
        menu.findItem(R.id.fall).setIcon(R.drawable.fall);
    else
        menu.findItem(R.id.fall).setVisible(false);

    if (fesConnectIconOn)
        menu.findItem(R.id.fesConnection).setIcon(R.drawable.fesconnect);
    else
        menu.findItem(R.id.fesConnection).setVisible(false);


}


    public void changeMenuItem(int resId, int draId){


    needToChangeMenuItem = true;
    resourceId = resId;
    drawableId = draId;

    invalidateOptionsMenu();        


}
0
MenuItem mi = (MenuItem) view.findViewById(R.id.opt_mnu_action);
mi.setIcon(R.drawable.ico_1);

In your Fragment's onCreateOptionsMenu, load this menu, and keep a reference to the menu item (it is not part of the fragment's view hierarchy so you can't use findViewById).

Whenerver you are ready to update the icon, use mi.setIcon(R.drawable.ico_1); and call invalidateOptionsMenu().

UPDATED:

return super.onCreateOptionsMenu(menu);

This will actually return false, since the base implementation doesn't do that. Instead skip it, or just call super.onCreateOptionsMenu(menu); first, do your stuff and then return true.

3
  • I tried to do something like that, I think. Please see my updated question. Thanks!
    – Igal
    Oct 4, 2013 at 18:02
  • Updated my answer, but that won't really solve your issue. Make sure action_button is not null right after action_button = menu.findItem(R.id.opt_mnu_action);.
    – f2prateek
    Oct 5, 2013 at 2:32
  • I didn't exactly understand, where am I supposed to do the super.onCreateOptionsMenu(menu);, then do my stuff (I assume by that you mean change the icon) and return true?
    – Igal
    Oct 5, 2013 at 12:56
0

First add actionOverFlowButtonStyle into your main theme

<style name="AppTheme" parent="AppBaseTheme">

    <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
</style>

Define the New style for action over flow button

<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_menu</item>
</style>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.