Gissipi_453 Gissipi_453 - 2 months ago 67
Android Question

Changing Background and text color of AppCompat Light DarkActionBar Theme on android

I have used AppCompat Light DarkActionBar Theme for my App.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>


enter image description here

Is it possible to change the background color and text color of this ActionBar ? If yes, how ?

Answer

The following code will allow you to style the action bar

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarStyle">@style/Widget.AppTheme.ActionBar</item>
    <item name="actionBarTheme">@style/ThemeOverlay.AppTheme.ActionBar</item>
    <item name="actionBarPopupTheme">@style/ThemeOverlay.AppTheme.PopupMenu</item>
</style>

<style name="Widget.AppTheme.ActionBar" parent="Widget.AppCompat.ActionBar.Solid">
    <!-- action bar background - without android prefix -->
    <item name="background">...</item>
    <!-- action bar elevation - without android prefix -->
    <item name="elevation">4dp</item>
</style>

<style name="ThemeOverlay.AppTheme.ActionBar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <!-- title text color -->
    <item name="android:textColorPrimary">...</item>
    <!-- subtitle text color -->
    <item name="android:textColorSecondary">?android:textColorPrimary</item>
    <!-- action menu item text color -->
    <item name="actionMenuTextColor">?android:textColorPrimary</item>
    <!-- action menu item icon color -->
    <item name="colorControlNormal">?android:textColorPrimary</item>
</style>

<style name="ThemeOverlay.AppTheme.PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
    <!-- popup menu background - NEVER "android:background" !!! in themes -->
    <item name="android:colorBackground">...</item>
    <!-- popup menu item text color -->
    <item name="android:textColorPrimary">...</item>
</style>

The following code will allow you to style the window

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- window background - NEVER "android:background" in themes !!! -->
    <item name="android:windowBackground">...</item>
    <item name="android:textColorPrimary">...</item>
    <item name="android:textColorSecondary">...</item>
</style>

The Lazy and Bad Way...

...that will break when you change color schemes.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarStyle">@style/Widget.AppTheme.ActionBar</item>
</style>

<style name="Widget.AppTheme.ActionBar" parent="Widget.AppCompat.ActionBar.Solid">
    <!-- action bar background - without android prefix -->
    <item name="background">...</item>
    <!-- action bar elevation - without android prefix -->
    <item name="elevation">4dp</item>
    <!-- title text color -->
    <item name="titleTextColor">?android:textColorPrimary</item>
    <!-- subtitle text color -->
    <item name="subtitleTextColor">?android:textColorSecondary</item>
</style>