50

Say I have a menu (options_menu.xml) similar to the following:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_login"
          android:title="Login"
          app:showAsAction="always"/>
</menu>

which I inflate into the new Toolbar item

mToolbar.inflateMenu(R.menu.options_home);

This results in something like

enter image description here

Is there a way to change this text color without using an image, changing the rest of the Toolbar text color, or by adding a custom view to the toolbar? Looking for an answer for minSdk 15 (appcompat).

Update:

My relevant style:

<style name="AppTheme" parent="AppTheme.Base">
    <item name="actionMenuTextColor">@color/ww_red</item>
</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/red</item>
    <item name="colorAccent">@color/theme_accent</item>

    <item name="android:textColor">@color/text_dark</item>

    <item name="android:colorEdgeEffect">@color/gray</item>
</style>
loeschg
  • 29,961
  • 26
  • 97
  • 150
  • Create an image keeping background color same as toolbar and set textColor whatever you want – Apurva Feb 17 '15 at 18:18
  • @Apurva I suppose that would work, but I'd like to find a solution that avoids needing custom assets for every text "button" that is added. – loeschg Feb 17 '15 at 18:22
  • @loeschg Will the `Login` menu item have different color from other menu items? – Vikram Feb 18 '15 at 16:15
  • 1
    I want any menu items that display *on* the toolbar (not overflow menu) to be a particular color. The ability to change color for each item would be nice, but not necessary. – loeschg Feb 18 '15 at 16:17
  • 1
    @loeschg In that case, use `@color/color_action_mode_text` as part of your main theme. – Vikram Feb 18 '15 at 16:20
  • @Vikram how is that different from what @koni suggested in the below answer? I was not able to get that to work. Does this require the `setSupportActionBar(toolbar)` method? I'm using the toolbar in a standalone manner. – loeschg Feb 18 '15 at 16:24
  • @loeschg `setSupportToolbar(Toolbar)` is not required. I am assuming you are adding the menu by calling `mToolbar.inflateMenu(int)`. Menu inflation still takes place through the MenuInflater which should honor `actionMenuTextColor` attribute. `koni` is using `android:` prefix which will not work with the appcompat v7. Since its a library, its attributes need to be addressed as any non-system attributes - without the prefix. – Vikram Feb 18 '15 at 16:50
  • @loeschg I get this [Link](http://postimg.org/image/q4zcdshgz/) when I use the style you added above. Just to be sure, confirm that you are assigning `AppTheme` (and not `AppTheme.Base`) to your activity. – Vikram Feb 18 '15 at 17:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71186/discussion-between-loeschg-and-vikram). – loeschg Feb 18 '15 at 17:24

6 Answers6

140

In your theme file you have to put this :

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

and apply this theme to your Toolbar view like this :

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/main_toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:background="?attr/colorPrimary"
   android:layout_gravity="top"
   app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
   android:theme="@style/AppTheme.ActionBar"/>

android:theme="@style/AppTheme.ActionBar" don't forget this line in your toolbar

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
koni
  • 1,805
  • 1
  • 14
  • 13
  • app:theme is deprecated. – hungryghost May 31 '15 at 09:52
  • app:popupTheme is deprecated – Shanison Sep 29 '15 at 09:18
  • By applying above solution no doubt its works fine, but it also changed Navigation drawer icon as black. While defining new style extend "AppTheme.AppBarOverlay" instead of "Theme.AppCompat.Light.DarkActionBar". This will not only change Menu item text color as well as keep default color of Navigation icon. – Rohan Patel Apr 26 '16 at 05:25
8

in your style file, place the following:

    <style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:titleTextStyle">@style/MyActionBar.TitleTextStyle 
        <item name="android:actionMenuTextAppearance">@style/MyActionBar.MenuTextStyle</item>
        <item name="android:actionMenuTextColor">@color/action_bar_red</item>  
    </style>

    <style name="MyActionBar.TitleTextStyle"
        parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">#F0F</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">24dip</item>
    </style>

    <style name="MyActionBar.MenuTextStyle"
        parent="android:style/TextAppearance.Holo.Widget.ActionBar.Menu">
        <item name="android:textColor">#F0F</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">24dip</item>
    </style>


You can make changes in the above style items to suit your requirements. I have not added separate styling for color though. As you might observe, I am just assigning red color (which I have declared in my colors file) to it. You may change as required.

loeschg
  • 29,961
  • 26
  • 97
  • 150
Saurabh Rajpal
  • 1,537
  • 9
  • 14
8

For me, this worked: Setting the "android:textColor"

<style name="AppTheme.ActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
        ...
        <!--This would set the menu item color-->
        <item name="android:textColor">#000</item>
        ...
 </style>
ayz4sci
  • 2,220
  • 1
  • 16
  • 17
3

Not sure if you can set specific backgrounds to separate MenuItems just through styling but you could just setActionView for your MenuItem.

First create a layout like item_av.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0000"
    android:padding="5dp">
    <TextView
        android:text="LOGIN"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textSize="12sp"
        android:gravity="center"/>
</LinearLayout>

Then set it to your MenuItem in onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);

    MenuItem item = menu.findItem(R.id.action_login);
    item.setActionView(R.layout.item_av);

    return true;
}
Simas
  • 43,548
  • 10
  • 88
  • 116
  • I'm not looking to set the background. Just the text color. Checking out your suggestion. – loeschg Feb 17 '15 at 21:04
  • @loeschg my bad. However this way you can simply set the TextView's textColor too. – Simas Feb 17 '15 at 21:11
  • Haha, I know. Just wanted to clarify in case this would somehow change the solution (including for others that may end up here). Seems kind of ridiculous that you can't change the text color of text that's already there. Note with this approach you need to set an onClickListener directly on the view instead of using the menuItemClickLIstener. – loeschg Feb 17 '15 at 21:15
  • how to link the new layout onclick event with onOptionsItemSelected? – Srneczek Jul 13 '16 at 16:16
3

to change menu item text color use below code

<style name="AppToolbar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:itemTextAppearance">@style/menu_item_color</item>
</style>

where

<style name="menu_item_color">
<item name="android:textColor">@color/app_font_color</item>
</style>
1

You can do it programmatically like that:

private AppCompatButton menuTextView; 

void initToolbar(){
    toolbar.inflateMenu(R.menu.your_menu);
    menuTextView = AppCompatButton(context);
    menuTextView.setTextColor(ContextCompat.getColor(
    getContext(), R.color.your_color));
    TypedValue outValue = new TypedValue();
    getContext().getTheme().resolveAttribute(
        R.attr.selectableItemBackgroundBorderless,
        outValue,
        true
    )
 toolbar.getMenu().getItem(0).setActionView(menuTextView);