Change Background color of the action bar using AppCompat

60,384

Solution 1

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background"  tools:ignore="NewApi">@color/red</item>
    <item name="background">@color/red</item>
</style>

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle"   tools:ignore="NewApi">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>

</style>

you need both android:background and background items. The former is for newer versions of android that support ActionBar natively. The latter is for older android versions.

EDIT

instead of <resources> use

<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

From SDK 21

to change Action Bar background color, add this to ActionBarTheme, and the two colours are to be different or it will not work (thanks to @Dre and @lagoman)

<item name="colorPrimary">@color/my_awesome_red</item> 
<item name="colorPrimaryDark">@color/my_awesome_darker_red</item>

Solution 2

Try this:

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>

</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">

    <item name="android:background">@color/red</item>
    <item name="background">@color/red</item>

</style>

Solution 3

Try this.

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary"> #6699FF </item>
</style>

You can change #6699FF color according to your choice (or use a color resource).

Solution 4

try to use

parent="Theme.AppCompat.Light"

instead of

parent="@style/Theme.AppCompat.Light"
Share:
60,384

Related videos on Youtube

Bombolo
Author by

Bombolo

Updated on June 07, 2020

Comments

  • Bombolo
    Bombolo almost 4 years

    I found some questions about this problem around the web. Unfortunately, everything i try so far, has been unsuccessful.

    Has the title say, i need to change the background color of my action bar.

    The project have a min sdk of 9, and max sdk of 19.

    I have create in my res/values folder, an xml file:

    red_actionbar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
            <item name="actionBarStyle">@style/MyActionBar</item>
        </style>
        <style name="MyActionBar"
               parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
            <item name="background">@color/red</item>
        </style>
    </resources>
    

    the colors.xml stored in res/values

    <resources>
        <color name="red">#FF0000</color>
    </resources>
    

    and the part of the manifest where i change the theme

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme" >
    

    But nothing changes. Where is the problem? The application accepts the code because if i change ths:

    <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
    

    to

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

    it does change the theme of my app, so the problem is in the style but I don't know how to solve it.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
            <item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
            <item name="actionBarStyle">@style/MyActionBar</item>
        </style>
        <style name="MyActionBar"
               parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
            <item name="android:background"  tools:ignore="NewApi">@color/red</item>
            <item name="background">@color/red</item>
        </style>
    </resources>
    
    • Blackbelt
      Blackbelt about 10 years
      what version of android has the device where do you test the code?
    • Bombolo
      Bombolo about 10 years
      Android 4.2.2 samsung s2 plus
    • angryITguy
      angryITguy about 8 years
      Theming is always tricky. It's a big mess and poorly documented. On another note, do you need to support a minimum Android SDK of 9? You will cover the vast majority of devices running 15 or higher.... just sayin..
  • Bombolo
    Bombolo about 10 years
    i can't use. Type mismatch: cannot convert from android.app.ActionBar to android.support.v7.app.ActionBar. my activity extends ActionBarActivity to support sdk 9
  • Bombolo
    Bombolo about 10 years
    Can't use android:actionBarStyle because my min-sdk is 9
  • Bombolo
    Bombolo about 10 years
    I get error if i use android:actionBarStyle android:actionBarStyle requires API level 11 (current min is 9)
  • Blackbelt
    Blackbelt about 10 years
    add tools:ignore="NewApi" to the items with the android: prefix.
  • Bombolo
    Bombolo about 10 years
    i have try, but i get this error, error: Error parsing XML: unbound prefix
  • Blackbelt
    Blackbelt about 10 years
    can you post the complete file?
  • Bombolo
    Bombolo about 10 years
    I have added the file.
  • Bombolo
    Bombolo about 10 years
    It works! thank you! but if i put ignore newapi can i have some problem in some devices?
  • Mladen
    Mladen over 9 years
    If you have updated SDK to API level 21, to change Action Bar background color, add this to ActionBarTheme: <item name=”colorPrimary”>@color/my_awesome_red</item> <item name=”colorPrimaryDark”>@color/my_awesome_darker_red</item> This is according to: http://android-developers.blogspot.com/2014/10/appcompat-v21‌​-material-design-for‌​-pre.html
  • Dre
    Dre over 9 years
    Just something to note, colorPrimary and colorPrimaryDark shouldn't be the same color. If they are, Android will not color the action bar.
  • N Jay
    N Jay over 9 years
    can i know how to change the title color as well ?
  • TechSpellBound
    TechSpellBound about 9 years
    Thanks! I spent 5 hours on this!!
  • TechSpellBound
    TechSpellBound about 9 years
    I don't understand why that edit was added to the accepted answer??
  • Cora
    Cora almost 9 years
    I couldn't tell you how many threads I poured through that didn't work until I found this one. Thanks a ton!
  • bashan
    bashan over 8 years
    Nothing seems to work. It is amazing how much time you should spend for doing a simple thing as setting background color of an action bar...
  • Tony
    Tony over 8 years
    just copy and paste into styles.xml?? No luck for me :(
  • Ivan
    Ivan about 8 years
    this answer is a duplicate of one of the above answers
  • NitinM
    NitinM over 5 years
    Worked For Me... great