2

I am trying to change menu item text color dynamically.

I have a solution that works for menu icons, it uses the color filter as follows:

Drawable drawable = menuItem.getIcon();

        if (drawable != null) {
            drawable.mutate();
            drawable.setColorFilter(new
                    PorterDuffColorFilter(Color.parseColor(color), PorterDuff.Mode.MULTIPLY));
        }
        menuItem.setIcon(drawable);

Output:enter image description here

I am unable to change the color of menu item text. To make this work I used the following code:

 SpannableString s = new SpannableString(menuItem.getTitle());
                s.setSpan(new ForegroundColorSpan(Color.parseColor(color)), 0, s.length(), 0);
                menuItem.setTitle(s);

Output:enter image description here

the color of "SAVE" is what I am trying to change.

Can anyone help me out with this?

3 Answers3

2

try this,

add this theme in style.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">    
    <item name="actionMenuTextColor">@color/text_color</item>    
</style>

and apply the theme to toolbar

 android:theme="@style/AppTheme"
Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23
  • Is there a way to specify text color from java code instead of specifying it in XML? e.g I am specifying actionMenuTextColor = white in style/XML & want to change it to RED in activity at runtime. How to do that? – Prashant Kawtikwar May 25 '17 at 08:48
  • check this https://stackoverflow.com/questions/8614293/android-get-view-reference-to-a-menu-item hope it will help you. – Pradeep Gupta May 25 '17 at 09:26
  • He wants to do it dynamically not the whole theme file! – JPM Dec 22 '22 at 17:33
1

add menu style

<style name="optionMenuTextApearance" parent="@style/Theme.Sherlock.Light">
    <item name="actionMenuTextColor">@color/white</item>
    <item name="android:actionMenuTextColor">@color/white</item>
</style>

called it in menu

<item name="android:itemTextAppearance">@style/optionMenuTextApearance</item>

Runtime change menu color

 @Override
public boolean onPrepareOptionsMenu(Menu menu) {
    boolean result = super.onPrepareOptionsMenu(menu);
    styleMenuButton();
    return result;
}

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
    }
}
Jai Khambhayta
  • 4,198
  • 2
  • 22
  • 29
  • Is there a way to specify text color from java code instead of specifying it in XML? e.g I am specifying actionMenuTextColor = white in style/XML & want to change it to RED in activity at runtime. How to do that? – Prashant Kawtikwar May 25 '17 at 08:49
  • I want to change the color of that "save" menu item in activity as shown below: `@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.menu_done, menu); MenuItem saveMenuItem = menu.findItem( R.id.action_done); // I want to change saveMenuItem text return super.onCreateOptionsMenu(menu); }` – Prashant Kawtikwar May 25 '17 at 10:25
  • 4
    View view = findViewById(R.id.YOUR_MENU_ITEM_ID_HERE); always returns null. – Prashant Kawtikwar May 25 '17 at 13:09
  • You have to find the Appear view, this finds nothing. – JPM Dec 22 '22 at 18:35
0

You should set your SpannableString on your Menu Item. Add the following line after you build a SpannableString:

menuItem.setTitle(s);

Since your menuItem.getTitle() is only providing a String value for making a SpannableString, not the reference as a menu item title.

viz
  • 1,247
  • 16
  • 27