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 Answers 3

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"
Sign up to request clarification or add additional context in comments.

3 Comments

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?
check this stackoverflow.com/questions/8614293/… hope it will help you.
He wants to do it dynamically not the whole theme file!
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
    }
}

4 Comments

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?
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); }
View view = findViewById(R.id.YOUR_MENU_ITEM_ID_HERE); always returns null.
You have to find the Appear view, this finds nothing.
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.

5 Comments

I have to change menu item text color not action bar title color.
@PrashantKawtikwar Sorry I was confused, try the edited answer.
Did you put your code in onCreateOptionsMenu? You should override that method in order to change the menu.
Yes. I wrote the code in onCreateOptionsMenu method.
That sounds weird. How does your xml layout look like?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.