Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Toolbar Menu Item color (non-hidden action)

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> 
like image 946
loeschg Avatar asked Feb 05 '15 20:02

loeschg


People also ask

How do I change the color of my toolbar menu?

Open the colors. xml file by navigating to the app -> res -> values -> colors. xml. Create a color tag inside the resources tag with a name and set a color with its hex code.

How do I replace my action bar toolbar?

You'll want to add android:fitsSystemWindows="true" to the parent layout of the Toolbar to ensure that the height of the activity is calculated correctly. When using the support library, make sure that you are importing android. support. v7.

How do I hide items on my toolbar?

The best way to hide all items in a menu with just one command is to use "group" on your menu xml. Just add all menu items that will be in your overflow menu inside the same group. Then, on your activity (preferable at onCreateOptionsMenu), use command setGroupVisible to set all menu items visibility to false or true.


1 Answers

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

like image 111
koni Avatar answered Oct 11 '22 06:10

koni