Rick Joe Rick Joe - 1 year ago 89
Android Question

setTheme colorprimary not working as background in custom toolbar?

I have a custom toolbar that I just added.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"


notice that I set background as colorPrimary.

In style.xml I have 2 styles:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

<style name="Yellow" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#ffff00</item>
<item name="colorPrimaryDark">#000000</item>
<item name="colorAccent">#ffff00</item>

my color.xml

<color name="colorPrimary">#000</color>
<color name="colorPrimaryDark">#fff</color>
<color name="colorAccent">#000</color>

I change color in activity using setTheme:


it works, except the BACKGROUND color that is always black, even if I set setTheme as Yellow.

any ideas why?

Answer Source

The below code is overriding the background color of Toolbar


. Remove android:theme attribute from Toolbar then it will work.

To change textcolor follow the below code:

<style name="CustomTextColor" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:textColor">Your Text Color</item>

Now set the theme to your Toolbar:


Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download