22

I have used AppCompat Light DarkActionBar Theme for my App.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">        
</style>

enter image description here

Is it possible to change the background color and text color of this ActionBar ? If yes, how ?

Gissipi_453
  • 1,250
  • 1
  • 25
  • 61
  • 1
    Do you want to change text and background color of the window or the action bar? – Eugen Pechanec Apr 30 '15 at 02:10
  • 2
    possible duplicate of [How do I change the background color of the ActionBar of an ActionBarActivity using XML?](http://stackoverflow.com/questions/8024706/how-do-i-change-the-background-color-of-the-actionbar-of-an-actionbaractivity-us) – Deividi Cavarzan Apr 30 '15 at 02:38

2 Answers2

62

The following code will allow you to completely style the action bar.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarStyle">@style/Widget.AppTheme.ActionBar</item>
    <item name="actionBarTheme">@style/ThemeOverlay.AppTheme.ActionBar</item>
    <item name="actionBarPopupTheme">@style/ThemeOverlay.AppTheme.PopupMenu</item>
</style>

Override the action bar style to change the background color or elevation.

<style name="Widget.AppTheme.ActionBar" parent="Widget.AppCompat.ActionBar.Solid">
    <!-- action bar background - without android prefix -->
    <item name="background">...</item>
    <!-- action bar elevation - without android prefix -->
    <item name="elevation">4dp</item>
</style>

Override the action bar theme to define special behavior for all views inside the action bar such as action items, ripple background color, text color.

<style name="ThemeOverlay.AppTheme.ActionBar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <!-- title text color -->
    <item name="android:textColorPrimary">...</item>
    <!-- subtitle text color -->
    <item name="android:textColorSecondary">?android:textColorPrimary</item>
    <!-- action menu item text color -->
    <item name="actionMenuTextColor">?android:textColorPrimary</item>
    <!-- action menu item icon color -->
    <item name="colorControlNormal">?android:textColorPrimary</item>
</style>

You can also change the popup theme.

<style name="ThemeOverlay.AppTheme.PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
    <!-- popup menu background - NEVER "android:background" !!! in themes -->
    <item name="android:colorBackground">...</item>
    <!-- popup menu item text color -->
    <item name="android:textColorPrimary">...</item>
</style>

On the other hand, the following code will allow you to style the window.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- window background - NEVER "android:background" in themes !!! -->
    <item name="android:windowBackground">...</item>
    <item name="android:textColorPrimary">...</item>
    <item name="android:textColorSecondary">...</item>
</style>

You can also change text color on the action bar directly. But beware! This will not afffect text color of menu items or icons or ripple color! This is probably not what you want in the long run.

<style name="Widget.AppTheme.ActionBar" parent="Widget.AppCompat.ActionBar.Solid">
    <!-- title text color -->
    <item name="titleTextColor">?android:textColorPrimary</item>
    <!-- subtitle text color -->
    <item name="subtitleTextColor">?android:textColorSecondary</item>
</style>
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • 18
    Why a simple thing like changing the title color has to be so damn complicated? I wonder how many Android developers actually understand the breakdown in this xml – Oded Regev Sep 13 '16 at 12:01
  • Thanks @EugenPechanec. What about changing `statusBar` color? – Antonio May 19 '17 at 01:43
  • @Antonio Hello, before asking a question always use the search bar. Someone else may have asked the same question already (and in this case they have). – Eugen Pechanec May 19 '17 at 07:16
  • Thanks, @EugenPechanec. Actually it was intended to suggest you to add it to your answer and have a more complete answer :) – Antonio May 19 '17 at 13:05
  • Hi @EugenPechanec : thank you for your answer, can you please explain what you mean by "The Lazy and Bad Way... that will break when you change color schemes."? I am not sure to understand in which case this will break. – Moussa May 29 '18 at 08:11
  • @Mouss Hmm, it's not apparent in the way I wrote the sample. If you used concrete colors as `titleTextColor` and `subtitleTextColor` and then changed theme from dark to light or the other way around then it would be noticeable. My line of thought is this: The action bar style should only ever use theme attributes so you shouldn't need to override `actionBarStyle`. Only `actionBarTheme` which supplies `colorPrimary`, `android:textColorPrimary` and `android:textColorSecondary`. I'll rewrite the answer so it's less biased. – Eugen Pechanec May 29 '18 at 19:24
1

Yes it is possible to change the color of the text on the action bar. Add this in your activity's onCreate:

 actionBar.setTitle(Html.fromHtml("<font color='#ffff00'>Your Title</font>"));  
onexf
  • 3,674
  • 3
  • 22
  • 36