RGS
RGS

Reputation: 4253

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"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">

    </android.support.v7.widget.Toolbar>

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>

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

my color.xml

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

I change color in activity using setTheme:

setTheme(colorId);

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

any ideas why?

Upvotes: 0

Views: 956

Answers (1)

tahsinRupam
tahsinRupam

Reputation: 6405

The below code is overriding the background color of Toolbar

android:theme="@style/ThemeOverlay.AppCompat.Dark" 

. 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>
</style>

Now set the theme to your Toolbar:

app:theme="@style/CustomTextColor"

Hope this helps.

Upvotes: 2

Related Questions