How to change the color of Action Bar in an Android App?

In this article, you will learn how to change the colour of the Action Bar in an Android App.
There are two ways to change color.
- By changing styles.xml file:
- Just go to res/values/styles.xml file
- edit the xml file to change the color of action bar.
- Code for styles.xml is given below
styles.xml
<resources>ÂÂÂ Â Â Â<!-- Base application theme. -->Â Â Â Â<stylename="AppTheme"parent="Theme.AppCompat.Light.DarkActionBar">Â Â Â Â Â Â Â Â<!-- Customize your theme here. -->Â Â Â Â Â Â Â Â<!-- This code is for changing the color of the bar. -->Â Â Â Â Â Â Â Â<!-- Type your colour code which you want to set in colorPrimary item -->Â Â Â Â Â Â Â Â<itemname="colorPrimary">#0F9D58</item>Â Â Â Â Â Â Â Â<itemname="colorPrimaryDark">@color/colorPrimaryDark</item>Â Â Â Â Â Â Â Â<itemname="colorAccent">@color/colorAccent</item>Â Â Â Â</style>Â Â Â Â<stylename="AppTheme.NoActionBar">Â Â Â Â Â Â Â Â<itemname="windowActionBar">false</item>Â Â Â Â Â Â Â Â<itemname="windowNoTitle">true</item>Â Â Â Â</style>Â Â Â Â<!-- Define other styles to fix theme -->Â Â Â Â<stylename="AppTheme.AppBarOverlay"parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>Â Â Â Â<stylename="AppTheme.PopupOverlay"parent="ThemeOverlay.AppCompat.Light"/></resources>activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?>ÂÂ<!--Relative Layout--><RelativeLayout   Âandroid:layout_width="match_parent"   Âandroid:layout_height="match_parent"   Âandroid:id="@+id/relativelayout">   Â<!--Text View-->  Â<TextView      Âandroid:layout_width="wrap_content"      Âandroid:layout_height="wrap_content"      Âandroid:id="@+id/textview"      Âandroid:textColor="#0F9D58"      Âandroid:textSize="32dp"      Âandroid:layout_centerInParent="true"/></RelativeLayout>MainActivity.java
packagecom.zambiatek.changecolor;importandroid.widget.TextView;importandroid.support.v7.app.AppCompatActivity;ÂÂpublicclassMainActivityextendsAppCompatActivity {   Â@Override   ÂprotectedvoidonCreate(Bundle savedInstanceState)   Â{       Âsuper.onCreate(savedInstanceState);       ÂsetContentView(R.layout.activity_main);       Â// Define text View       ÂTextView t = findViewById(R.id.textview);       Ât.setText("Geeks for Geeks");   Â}} - Through Java file by defining ActionBar object:
- Define object for ActionBar and colorDrawable class
- set color using setBackgroundDrawable function with colorDrawable object as its parameter.
- Here is complete code for MainActivity.java
MainActivity.java
packagecom.zambiatek.changecolor;ÂÂimportandroid.support.v7.app.ActionBar;importandroid.graphics.Color;importandroid.graphics.drawable.ColorDrawable;importandroid.support.v7.app.AppCompatActivity;ÂÂpublicclassMainActivityextendsAppCompatActivity {   Â@Override   ÂprotectedvoidonCreate(Bundle savedInstanceState)   Â{       Âsuper.onCreate(savedInstanceState);       ÂsetContentView(R.layout.activity_main);       Â// Define ActionBar object       ÂActionBar actionBar;       ÂactionBar = getSupportActionBar();       Â// Define ColorDrawable object and parse color       Â// using parseColor method       Â// with color hash code as its parameter       ÂColorDrawable colorDrawable           Â=newColorDrawable(Color.parseColor("#0F9D58"));       Â// Set BackgroundDrawable       ÂactionBar.setBackgroundDrawable(colorDrawable);   Â}}activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?>ÂÂ<!--Relative Layout--><RelativeLayout   Âandroid:layout_width="match_parent"   Âandroid:layout_height="match_parent"   Âandroid:id="@+id/relativelayout">   Â<!--Text View-->  Â<TextView      Âandroid:layout_width="wrap_content"      Âandroid:layout_height="wrap_content"      Âandroid:textColor="#0F9D58"      Âandroid:textSize="30dp"      Âandroid:text="Geeks for Geeks"      Âandroid:layout_centerInParent="true"/></RelativeLayout>
Output:




