How to change ActionBar's action text color using AppCompat
Asked Answered
R

3

16

I am trying to change the text color of a action menu item on the action bar. Using app compat. Also the overflow icon doesnt change too. Here is my custom styled styles.xml files.

res/values/styles.xml

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    <!-- Setting values in the default namespace affects API levels 7-13 -->
    <item name="actionBarStyle">@style/MyStyledActionBar</item>
</style>

<style name="MyStyledActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <!-- Setting values in the default namespace affects API levels 7-13 -->
    <item name="background">@drawable/bg_action_bar</item>
    <item name="titleTextStyle">@style/MyActionBarTitleText</item>
    <item name="actionMenuTextAppearance">@style/MyActionBarMenuText</item>
    <item name="actionMenuTextColor">@style/MyActionBarMenuText</item>
    <item name="actionOverflowButtonStyle">@style/MyActionButtonOverFlow</item>
</style>

<style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/font_half_white</item>
</style>

<style name="MyActionBarMenuText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu">
    <item name="android:textColor">@color/font_half_white</item>
</style>

<style name="MyActionButtonOverFlow" parent="@style/Widget.AppCompat.Light.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_action_search</item>
</style>

res/values-14/styles.xml

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    <!-- Setting values in the android namespace affects API levels 14+ -->
    <item name="android:actionBarStyle">@style/MyStyledActionBar</item>
</style>

<style name="MyStyledActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <!-- Setting values in the android namespace affects API levels 14+ -->
    <item name="android:background">@drawable/bg_action_bar</item>
    <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
    <item name="android:actionMenuTextAppearance">@style/MyActionBarMenuText</item>
    <item name="android:actionMenuTextColor">@style/MyActionBarMenuText</item>
    <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverFlow</item>
</style>

<style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/font_half_white</item>
</style>

<style name="MyActionBarMenuText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu">
    <item name="android:textColor">@color/font_half_white</item>
</style>

<style name="MyActionButtonOverFlow" parent="@style/Widget.AppCompat.Light.Base.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_action_search</item>
</style>

res/colors.xml #F3F3F3

The text color still black doesnt change to white. Has anybody successfully changed the action menu text color. Please suggest some insights. I am testing on 4.4 Nexus5 device.

enter image description here

Razo answered 26/3, 2014 at 4:42 Comment(0)
H
17

Use

<item name="android:actionMenuTextColor">@color/font_half_white</item>
// added style directly 

Instead of

<item name="android:actionMenuTextColor">@style/MyActionButtonStyle</item>

in res/values-14/styles.xml

In res/values/styles.xml

<item name="actionMenuTextColor">@color/font_half_white</item>

I used a Red Color for testing. See the Action menu Item Home that is red in the snap

enter image description here

Edit:

Full Code

In res/values-14/styles.xml

<resources>

    <!--
        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
            <item name="android:actionMenuTextColor">@color/red</item>
    </style>

</resources>

Then in res/values/stles.xml

  <resources>
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!-- API 11 theme customizations can go here. -->
         <item name="actionMenuTextColor">@color/red</item>
    </style>

</resources>

In manifest

android:theme="@style/AppBaseTheme" >

Another snap

enter image description here

Hygrometric answered 26/3, 2014 at 5:18 Comment(8)
Would you share your AppTheme style and how it is set in AndroidManifest.xml file.Razo
<style name="AppTheme" parent="@style/Theme.AppCompat.Light"> <item name="actionBarStyle">@style/MyStyledActionBar</item> </style>Razo
@Razo add it to AppTheme itselfHygrometric
@Razo posted complete code. rest is upto you to fix. Also do't post links. Post the relevant code in your post. Nobody will waste time looking at an external source.Hygrometric
worked for me but needed to put the item inside a theme that was applied per activity, not inside the theme applied for the whole app. I hate this actionbar more than anything...Discursion
@Hygrometric is it possible to use somehow a selector for the menu item? I'd like to highlight the latest selected option. Any hint?Natalyanataniel
@Natalyanataniel no i haven't tried it. You style your menu items. But not sure of using a selector.Hygrometric
it is not possible I think. I ended up changing it manually :/Natalyanataniel
J
1

You can use this to change the text of the Action Bar irrespective of your appcompat version.Give the desired color's hex code and add this in the onCreate().

actionBar.setTitle(Html.fromHtml("<font color='#ff0000'>Some Title</font>"));
Jackstay answered 23/10, 2016 at 7:56 Comment(0)
L
0
  1. Check that in your layout xml file you don't have an android:theme attribute specified for your <Toolbar>, which would have priority over the main theme

  2. Override your actionBarStyle attribute as pointed out everywhere over the internets.

Lefebvre answered 20/1, 2017 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.