This post is to explain how you can change the Default Color of an Android App's or a particular Activity,
This can be done both using Style XML code and Programmatically using Java or Kotlin Code.
Example 1: Change Android Activity Title Bar Color using Style XML
- Open your activity_ .xml file under /res/layouts
- Click on Code
- Look for androidx.appcompat.widget.Toolbar
- Now look for attribute android:background=
- Now change its value to any hex color code you want example: #eeeeee (it better you add this to colors.xml and use reference instead example: @color/gray
- Run your App on the emulator or device you should see that the titlebar/Toolbar color has changed.
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/gray"
app:popupTheme="@style/AppTheme.PopupOverlay" />
Code Snippet: colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
<color name="gray">#eee</color>
</resources>
Example 2: Programmatically using Java/Kotlin Code:
Add the below code in your Activity java class in onCreate() method,
ActionBar aBar;
aBar= getSupportActionBar();
ColorDrawable cd = new ColorDrawable(Color.parseColor("#eee"));
actionBar.setBackgroundDrawable(cd);
This is not an AI-generated article but is demonstrated by a human.
Please support independent contributors like Code2care by donating a coffee.
Buy me a coffee!

Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!