JavaGrassHopper
JavaGrassHopper

Reputation: 151

Changing a Menu Item Icon during run time

How can I change an Menu Item icon during run time according to an If statement ? This is the code that I have and it results in a crash.

public boolean onPrepareOptionsMenu(Menu Item) {
    if (favorite == true){
        itema = (MenuItem) findViewById(R.id.action_search);
        itema.setIcon(R.drawable.ic_action_importants);
    }
    return true;
}

Upvotes: 0

Views: 326

Answers (1)

Pring
Pring

Reputation: 61

You are calling the Activity's findViewById, you should have more luck by calling Menu findItem.

public boolean onPrepareOptionsMenu(Menu menu)
{
    MenuItem myMenuItem = menu.findItem(R.id.myMenuItemId);
    myMenuItem.setIcon(R.drawable.ic_myAction);
    return true;
}

Upvotes: 1

Related Questions