After searching a lot and testing solutions could not customize action bar. This post is really good but didn't help me. I'm wondering why doing this simple work is really hard in android.
I created a sample project with Basic Activity in Android Studio, API 17, and Android 4.2 and want to customize the action bar with a white background and dark text color, without changing "colorPrimary" in style.xml.
Below is my codes but didn't work, of course, I have tested other ways but didn't work, this is my latest test codes. Is it possible because of the API version?
This is in the color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#028a58</color>
<color name="colorPrimaryDark">#005c3a</color>
<color name="colorAccent">#03DAC5</color>
//I added these lines
<color name="colorWhite">#FFFFFF</color>
<color name="colorDark">#4E4E4E</color>
</resources>
In the style.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background" >@color/colorWhite</item>
<item name="background">@color/colorWhite</item>
<item name="android:textColor">@color/colorDark</item>
</style>
</resources>
Updated:
activity_main.xml:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Using an ActionBar
you can use the actionBarStyle
attribute in your app theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">@color/...</item>
</style>
or the actionBarTheme
attribute:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarTheme">@style/ThemeOverlay.ActionBar</item>
</style>
<style name="ThemeOverlay.ActionBar" parent="">
<item name="colorPrimary">@color/....</item>
</style>
Using a Toolbar
you can use:
android:background
attribute in the layout: <androidx.appcompat.widget.Toolbar
android:background="@color/...."
... />
android:theme
attribute: <androidx.appcompat.widget.Toolbar
android:theme="@style/ThemeOverlay.ActionBar"
... />