3

I'm trying customize the ActionBar in my app. I want to change background color and text color.

My styles.xml is as below:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme.NoActionBar" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>

        <item name="android:actionBarStyle">@style/AppTheme.ActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/AppTheme.ActionBar</item>
    </style>

    <style name="AppTheme.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
        <item name="android:background">@color/actionbar_background</item>
        <item name="android:titleTextStyle">@style/AppTheme.ActionBar.TitleTextStyle</item>

        <!-- Support library compatibility -->
        <item name="background">@color/actionbar_background</item>
        <item name="titleTextStyle">@style/AppTheme.ActionBar.TitleTextStyle</item>
    </style>

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

</resources>

In my AndroidManifest.xml I added android:theme="@style/AppTheme.NoActionBar". In my activity I added the action bar with code

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:theme="@style/AppTheme.ActionBar" />

In result I have the action bar with correct color but incorrect text color.

What's wrong?

Note: I'm using AppCompat, not Holo or other.

Sufian
  • 6,405
  • 16
  • 66
  • 120
Yara_M
  • 633
  • 9
  • 29

5 Answers5

2

This is happening because your Toolbar's theme is set to light, hence the Android picks a dark (black) text colour.

You need to update it to following:

<style name="AppTheme.ActionBar" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="titleTextColor">#FF0001</item>
    <item name="subtitleTextColor">#FF0001</item>
    <!-- your other theme attributes -->
</style>

Notes:

  1. For Toolbar themes, use ThemeOverlay instead of Theme.AppCompat. Related - When should one use Theme.AppCompat vs ThemeOverlay.AppCompat?,
  2. replace #FF0001 with any colour you like. This is the colour of your title text.
Community
  • 1
  • 1
Sufian
  • 6,405
  • 16
  • 66
  • 120
  • text colour only? Or back arrow too? – Sufian Sep 01 '16 at 13:12
  • Uhm. I don't see any "back arrow" on action bar. – Yara_M Sep 01 '16 at 13:19
  • Thank you, it works. And line `@style/AppTheme.ActionBar.TitleTextStyle` is useless now, isn't it? – Yara_M Sep 01 '16 at 13:40
  • One more question. I found what my actionbar popup menu is dark now and text on it is the same colour. It makes me sad. If you know how fix it, please tell. – Yara_M Sep 06 '16 at 13:55
  • @ziwert you need to add this line to your `Toolbar` in XML: `app:popupTheme="@style/ThemeOverlay.AppCompat.Light"`. This means that your popup will use light theme. – Sufian Sep 06 '16 at 14:51
  • @ziwert post your `Toolbar` tag from the XML. – Sufian Sep 06 '16 at 15:25
  • I added a new question here http://stackoverflow.com/questions/39362681/actionbar-popup-menu-style – Yara_M Sep 07 '16 at 06:36
0

maybe you should remove actionBarTabTextStyle from AppTheme.NoActionBar

hope it will help...

0

Why don't you directly use

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

inside your custom MyActionBar style defination.

Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45
0

Instead of app:theme i am using android:theme which works for me.

Give it a try.

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/MyActionBar" />              <----------Here
Darshan Miskin
  • 844
  • 14
  • 25
  • I think this should solve it.. `titleTextColor` instead of `textColor` `` – Darshan Miskin Sep 01 '16 at 12:15
0

You can change the ActionBar text and background color through java code

yourToolbar.setTitleTextColor(Color.WHITE); yourToolbar.setBackgroundColor(Color.YOUR_COLOR);

Chandan
  • 187
  • 1
  • 8