19

I am using the app theme Theme.Black in my app. In this theme the action bar is gray. How I can change color of my action bar? This is my attempt:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="mytheme" parent="@android:style/Theme.Black" >

    </style>
    <style name="Widget.MyApp.ActionBar" parent="@android:style/Widget.ActionBar">
        <item name="android:background">@android:color/black</item>
    </style>



</resources>

However, it doesn't work. Any ideas?

Alessandro Cuttin
  • 3,822
  • 1
  • 30
  • 36
user1302569
  • 7,131
  • 13
  • 46
  • 66

9 Answers9

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

it worked for me here

Community
  • 1
  • 1
user3225831
  • 477
  • 9
  • 16
  • 4
    Thank you. It worked for me. getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.material_blue_gray1))); – Ataru Apr 26 '15 at 08:54
  • Per my experience, setting this has a drawback, i.e. the action bar menu color will not change. – Elye Jan 08 '16 at 11:07
  • It works perfect just edit the comment like @Ataru wrote. – f.trajkovski Mar 20 '18 at 12:14
10
<style name="AppTheme" parent="AppBaseTheme">

    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#C1000E</item>
    <item name="android:titleTextStyle">@style/AppTheme.ActionBar.TitleTextStyle</item>
</style>

<style name="AppTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.StatusBar.Title">
    <item name="android:textColor">#E5ED0E</item>
</style>

I have Solved Using That.

zaptech
  • 131
  • 1
  • 15
7
 ActionBar actionBar;

 actionBar = getActionBar(); 
 ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#93E9FA"));     
 actionBar.setBackgroundDrawable(colorDrawable);
Stephen
  • 9,899
  • 16
  • 90
  • 137
2

Just simply go to res/values/styles.xml file and edit the xml file to change the color of xml file .Here is the sample code

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->

// below code is for changing the color of action bar

    <item name="colorPrimary">"type your color code here. eg:#ffffff"</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

Hope it will help you...

2

Updated code:

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.your_color)));
MorZa
  • 2,215
  • 18
  • 33
1

If you use Android default action bar then. If you change from java then some time show previous color.

enter image description here

Example

Then your action bar code inside "app_bar_main". So go inside app_bar_main.xml and just add Background.

Example

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#33691e"   <!--use your color -->
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main1" />

Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
0

Maybe this can help you also. It's from the website:

http://nathanael.hevenet.com/android-dev-changing-the-title-bar-background/

First things first you need to have a custom theme declared for your application (or activity, depending on your needs). Something like…

<!-- Somewhere in AndroidManifest.xml -->
<application ... android:theme="@style/ThemeSelector">

Then, declare your custom theme for two cases, API versions with and without the Holo Themes. For the old themes we’ll customize the windowTitleBackgroundStyle attribute, and for the newer ones the ActionBarStyle.

<!-- res/values/styles.xml -->
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="ThemeSelector" parent="android:Theme.Light">
        <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
    </style>

    <style name="WindowTitleBackground">     
        <item name="android:background">@color/title_background</item>
    </style>

</resources>

<!-- res/values-v11/styles.xml -->
<?xml version="1.0" encoding="utf-8"?>
<resources>

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

    <style name="ActionBar" parent="android:style/Widget.Holo.ActionBar">     
        <item name="android:background">@color/title_background</item>
    </style>

</resources>

That’s it!

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
  • What about `values-v14` folder? – Si8 Aug 20 '13 at 01:59
  • What do you mean, what's about? – Bruno Bieri Aug 25 '13 at 12:51
  • I am sorry. I meant, how do I change using Java instead of XML? – Si8 Aug 25 '13 at 13:37
  • If I'm understanding you correctly you need to change App theme during runtime otherwise it does not make sense to use java instead of XML definitions. For that you can have a look here: http://stackoverflow.com/questions/2482848/how-to-change-current-theme-at-runtime-in-android – Bruno Bieri Aug 25 '13 at 14:23
0

if you have in your layout activity_main

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

you have to put

in your Activity this code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setBackgroundColor(Color.CYAN);
}
Cabezas
  • 9,329
  • 7
  • 67
  • 69
0

onclick buttons c2 to fetch and change the color of action bar and notification bar according to button's background, hope this helps

int color = c2.getBackground()).getColor();
int colorlighter = -color * 40 / 100 + color;
getWindow().setNavigationBarColor(colorlighter);
getWindow().setStatusBarColor(colorlighter);
ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable(color));

colorlighter is used to set color of notification bar a little lighter than action bar
color is the background color of button c2, according to which we change our color.

barbsan
  • 3,418
  • 11
  • 21
  • 28