Search code examples
androidandroid-actionbar

Change MenuItem text color programmatically


So I have a menu item, that's defined as:

<item
    android:id="@+id/action_live"
    android:title="@string/action_live"
    android:orderInCategory="1"
    app:showAsAction="ifRoom|withText" />

It shows as text, as you can see below:

Screenshot 1

And I want to programmatically change the "LIVE" text color. I've searched for a while and I found a method:

With globally defined:

private Menu mOptionsMenu;

and:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    mOptionsMenu = menu;
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

I do:

MenuItem liveitem = mOptionsMenu.findItem(R.id.action_live);
SpannableString s = new SpannableString(liveitem.getTitle().toString());
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
liveitem.setTitle(s);

But nothing happens!

If I do the same for an item of the overflow menu, it works:

Screenshot 2

Is there some limitation for app:showAsAction="ifRoom|withText" items? Is there any workaround?

Thanks in advance.


Solution

  • Bit late to the party with this one, but I spent a while working on this and found a solution, which may be of use to anyone else trying to do the same thing. Some credit goes to Harish Sridharan for steering me in the right direction.

    You can use findViewById(R.id.MY_MENU_ITEM_ID) to locate the menu item (provided that the menu had been created and prepared), and cast it to a TextView instance as suggested by Harish, which can then be styled as required.

    public class MyAwesomeActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        super.onCreate(savedInstanceState);
    
        // Force invalidatation of the menu to cause onPrepareOptionMenu to be called
        invalidateOptionsMenu();
    }
    
    private void styleMenuButton() {
        // Find the menu item you want to style
        View view = findViewById(R.id.YOUR_MENU_ITEM_ID_HERE);
    
        // Cast to a TextView instance if the menu item was found
        if (view != null && view instanceof TextView) {
            ((TextView) view).setTextColor( Color.BLUE ); // Make text colour blue
            ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_SP, 24); // Increase font size
        }
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean result = super.onPrepareOptionsMenu(menu);
        styleMenuButton();
        return result;
    }
    

    }

    The trick here is to force the menu to be invalidated in the activity's onCreate event (thereby causing the onPrepareMenuOptions to be called sooner than it would normally). Inside this method, we can locate the menu item and style as required.