Android Action Bar setting background color

By | April 16, 2015

 In Android Application if you don’t customize the action bar then the default icon and background color is set , if you want to change the icon and background color for action bar then you need to customize the style inside the res directory of your android project .

In Code

// setting background color
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor(“#ffff0000”));
getActionBar().setBackgroundDrawable(colorDrawable);

//setting icon
getActionBar().setIcon(R.drawable.icon);

In XML

Declare set of color constants inside the res/values/colors.xml

file : res/values/colors.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
     <color name=”Red”>#FF0000</color>
     <color name=”Green”>#008000</color>
     <color name=”Blue”>#0000FF</color>
     <color name=”LightGreen”>#98BD49</color>
     <color name=”Orange”>#FA8400</color>
     <color name=”Pink”>#F50057</color>
     <color name=”Yellow”>#FFF0F0</color>
     <color name=”Black”>#000000</color>
     <color name=”White”>#FFFFFF</color>
</resources>

Adding the below style inside the res/values/style.xml , res/values-v11/style.xml , res/values-v14/style.xml

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

    <!– Application theme. –>
    <style name=”AppTheme” parent=”@android:style/Theme.Holo.Light.DarkActionBar”>
        <item name=”android:actionBarStyle”>@style/MyActionBar</item>
    </style>

    <!– ActionBar styles –>
    <style name=”MyActionBar” parent=”@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse”>
        <item name=”android:background”>@color/Pink</item>
    </style>  

</resources>

Inside manifest file for application tag set the theme property with above defined style name 

<application
android:theme=”@style/AppTheme”
———
———