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

23 Feb, 2021
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 fileedit the xml file to change the color of action bar.Code for styles.xml is given belowstyles.xmlactivity_main.xmlMainActivity.javastyles.xml<resources>      <!-- Base application theme. -->    <style name="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 -->        <item name="colorPrimary">#0F9D58</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>    <!-- Define other styles to fix theme -->    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /></resources>activity_main.xml<?xml version="1.0" encoding="utf-8"?>  <!--Relative Layout--><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    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.javapackage com.geeksforgeeks.changecolor;import android.widget.TextView;import android.support.v7.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(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 classset color using setBackgroundDrawable function with colorDrawable object as its parameter.Here is complete code for MainActivity.javaMainActivity.javaactivity_main.xmlMainActivity.javapackage com.geeksforgeeks.changecolor;  import android.support.v7.app.ActionBar;import android.graphics.Color;import android.graphics.drawable.ColorDrawable;import android.support.v7.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {      @Override    protected void onCreate(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            = new ColorDrawable(Color.parseColor("#0F9D58"));          // Set BackgroundDrawable        actionBar.setBackgroundDrawable(colorDrawable);    }}activity_main.xml<?xml version="1.0" encoding="utf-8"?>  <!--Relative Layout--><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    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>
By changing styles.xml file:Just go to res/values/styles.xml fileedit the xml file to change the color of action bar.Code for styles.xml is given belowstyles.xmlactivity_main.xmlMainActivity.javastyles.xml<resources>      <!-- Base application theme. -->    <style name="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 -->        <item name="colorPrimary">#0F9D58</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>    <!-- Define other styles to fix theme -->    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /></resources>activity_main.xml<?xml version="1.0" encoding="utf-8"?>  <!--Relative Layout--><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    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.javapackage com.geeksforgeeks.changecolor;import android.widget.TextView;import android.support.v7.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // Define text View        TextView t = findViewById(R.id.textview);        t.setText("Geeks for Geeks");    }}
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
activity_main.xml
MainActivity.java
<resources>      <!-- Base application theme. -->    <style name="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 -->        <item name="colorPrimary">#0F9D58</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>    <!-- Define other styles to fix theme -->    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /></resources>
<?xml version="1.0" encoding="utf-8"?>  <!--Relative Layout--><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    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>
package com.geeksforgeeks.changecolor;import android.widget.TextView;import android.support.v7.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(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 classset color using setBackgroundDrawable function with colorDrawable object as its parameter.Here is complete code for MainActivity.javaMainActivity.javaactivity_main.xmlMainActivity.javapackage com.geeksforgeeks.changecolor;  import android.support.v7.app.ActionBar;import android.graphics.Color;import android.graphics.drawable.ColorDrawable;import android.support.v7.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {      @Override    protected void onCreate(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            = new ColorDrawable(Color.parseColor("#0F9D58"));          // Set BackgroundDrawable        actionBar.setBackgroundDrawable(colorDrawable);    }}activity_main.xml<?xml version="1.0" encoding="utf-8"?>  <!--Relative Layout--><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    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>
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
activity_main.xml
package com.geeksforgeeks.changecolor;  import android.support.v7.app.ActionBar;import android.graphics.Color;import android.graphics.drawable.ColorDrawable;import android.support.v7.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {      @Override    protected void onCreate(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            = new ColorDrawable(Color.parseColor("#0F9D58"));          // Set BackgroundDrawable        actionBar.setBackgroundDrawable(colorDrawable);    }}
<?xml version="1.0" encoding="utf-8"?>  <!--Relative Layout--><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    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:
Default color of action Bar:
In Main Activity color of Action Bar is changed to hash code defined in above code.
Android-Bars
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.