Change text color of option menu when we use app:showAsAction="always"
Asked Answered
S

2

7

I am using toolbar and use option for corresponding actions. My problem is this I want to show "SAVE" text on toolbar with white color, I apply many styles but it always appear as black. But when option appear as popup then text color is always white . I google this a lot , but have no solution.

Here is my code :

toolbar

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/my_awesome_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:theme="@android:style/ThemeOverlay.Material.Light"
    app:popupTheme="@style/Theme.AppCompat.NoActionBar"
    android:background="@drawable/tb_header"
    android:minHeight="?attr/actionBarSize"/>

styles

 <style name="MStyle" parent="Theme.AppCompat.Light.NoActionBar">

 <item name="android:windowBackground">@android:color/white</item>
        <item name="android:colorBackground">@android:color/black</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:textAllCaps">false</item>
        <item name="android:popupMenuStyle">@style/MyPopupMenu</item>
        <item name="android:panelBackground">@android:color/holo_green_light</item>
        <item name="android:actionBarWidgetTheme">@style/AppTheme</item>

    </style>

<style name ="MyPopupMenu" parent="android:Theme.Holo.Light">
        <item name="android:popupBackground">@android:color/holo_green_light</item>
    </style>


****Option menu**** 

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.doubtfire.userprofiles.ParentProfileActivity">
    <item
        android:id="@+id/action_skip"
        android:title="@string/menu_str_skip"

        android:orderInCategory="100"
        app:showAsAction="always" />
</menu>


****I even apply code at onPrepareOptionMenu**** 



   @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            for (int i=0; i<menu.size(); i++) {
                MenuItem mi = menu.getItem(i);
                String title = mi.getTitle().toString();
                Spannable newTitle = new SpannableString(title);
                newTitle.setSpan(new ForegroundColorSpan(Color.WHITE), 0, newTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                mi.setTitle(newTitle);
            }
            return true;
        }
Sonics answered 21/4, 2015 at 8:8 Comment(0)
K
8

Use

<item name="android:actionMenuTextColor">#FFFFFF</item>

in MStyle, as is shown below:

<style name="MStyle" parent="Theme.AppCompat.Light.NoActionBar">

  ...

  <item name="android:actionMenuTextColor">#FFFFFF</item>
  <!-- for appcompat-->
  <item name="actionMenuTextColor">#FFFFFF</item>
</style>

This change colour of menu item when SAVE item will be visible on ActionBar

Kinakinabalu answered 21/4, 2015 at 8:32 Comment(9)
This is not Working :(Sonics
Maybe I didn't understood you. Do you want to change text of menu item on Action bar? That right? If yes this should works. Did you added your style to the Activity in Manifest?Kinakinabalu
Yes , I want extactly what you say , and I added this theme to while app in application tag asSonics
<application android:name=".utils.AppData" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/MStyle" >Sonics
Try add it for activity tag : <activity android:name=".YourActivity" android:theme="@style/MStyle"Kinakinabalu
It is very strange for me because it works fine in my example.Kinakinabalu
Let us continue this discussion in chat.Sonics
@KonradKrakowiak this working in 5.0 but not in 4.0.. ?how can do the same for all versionArbitrary
any concrete example . there is a lot of confusion in different versions api .Shoring
A
2

To change the color of the text in Toolbar menu use below code.

<style name="MStyle" parent="Theme.AppCompat.Light.NoActionBar">
         ....   

           <item name="actionMenuTextColor">#FFFFFF</item>

</style>
Apollonian answered 21/4, 2015 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.