Changing options menu icon in actionbar depending on an open Fragment

Igal picture Igal · Oct 4, 2013 · Viewed 26k times · Source

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...

Answer

ksu picture ksu · Dec 16, 2013

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();        


}