Android: how to change action bar background color?

12,341

Solution 1

To apply the theme to all activities, change the style.xml like this:

<style name="AppTheme" parent="android:Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ActionBarTheme</item>
</style>

<style name="ActionBarTheme" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/action_bar</item>
</style>

And in colours.xml, use:

<color name="action_bar">#BA0000</color>

And in the manifest:

<application
    android:theme="@style/AppTheme" >

Solution 2

if you want change the color of ActionBar just do this:

 ActionBar bar = getActionBar();
 bar.setBackgroundDrawable(new ColorDrawable("COLOR"));

see the following link for more info

Share:
12,341
Jennifer
Author by

Jennifer

A university student who has just started to learn about programming.

Updated on June 14, 2022

Comments

  • Jennifer
    Jennifer almost 2 years

    The color of my android application is black. I wish to change it to other color. What is the easiest way to do it? I have tried to make a theme from this link

    When the application is opened, I could see that the action bar has changed the color from black to other color but in about 3 seconds, there is a message: Unfortunately, .... has stopped. I wish to use an easier way to may it. Any help is greatly appreciated.

  • Jennifer
    Jennifer over 10 years
    I am just a very beginner. May you be so kind to tell me where shall I add the above code? In the xml file? Or in the java file?
  • Shayan Pourvatan
    Shayan Pourvatan over 10 years
    in the java file and in onCreat() method
  • Jennifer
    Jennifer over 10 years
    First of all, I wish to thank you for your kind reply. I tried your suggestion and in the styles.xml file, I get this error message:error: Error: No resource found that matches the given name (at 'android:background' with value '@color/action_bar'). I think I might have made some mistakes and it would be great if you could give me some suggestions. Thanks in advance.
  • Jennifer
    Jennifer over 10 years
    I changed the @color/action_bar to a hex color code and it works fine. I feel really grateful to you. However, I still hope that you could point me the possible reason for the problem I have stated in the above comment. :)
  • Arshia
    Arshia almost 9 years
    Only problem with this code is that it causes a NullPointerException error. Do you know any ways to avoid this?
  • Shayan Pourvatan
    Shayan Pourvatan almost 9 years
    @Arshia you can check bar is not equal to null, you need check that ( if ( bar != null ) then set background
  • Arshia
    Arshia almost 9 years
    @shayanpourvatan That's awesome, thanks!