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--><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/relativelayout"><!--Text View--><TextViewandroid: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 {@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Define text ViewTextView 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 {@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Define ActionBar objectActionBar actionBar;actionBar = getSupportActionBar();// Define ColorDrawable object and parse color// using parseColor method// with color hash code as its parameterColorDrawable colorDrawable=newColorDrawable(Color.parseColor("#0F9D58"));// Set BackgroundDrawableactionBar.setBackgroundDrawable(colorDrawable);}}activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><!--Relative Layout--><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/relativelayout"><!--Text View--><TextViewandroid: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:




