kevin-lisnr
kevin-lisnr

Reputation: 43

appcompat v22.1 widget tinting doesn't show pre 5.0

I'm using the AppCompat lib v22.1 to give some material design to multiple android versions. If I use the new widget tinting feature, it does not show up in Kitkat even with AppCompat.

in layout:

<Button
            android:id="@+id/mybtn"
            android:text="Test"
            android:theme="@style/MyTheme.BlueButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

in theme xml:

<style name="MyTheme.BlueButton">
    <item name="colorButtonNormal">@color/blue</item>
</style>

Is this something I'm doing wrong or a bug in AppCompat?

Upvotes: 2

Views: 399

Answers (2)

Eugen Pechanec
Eugen Pechanec

Reputation: 38223

I was able to achieve desired results on Kitkat and Lollipop by using the following setup:

build.gradle (partial)

dependencies {
    compile 'com.android.support:appcompat-v7:22.1.1'
}

values/styles.xml

<resources>

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

    <style name="ThemeOverlay.AppTheme.Blue" parent="ThemeOverlay.AppCompat.Dark">
        <!-- ThemeOverlay themes define only some attributes, those which make
            backgrounds dark and highlights and texts light for ThemeOverlay.AppCompat.Dark
            or the other way around for .Light. The rest is taken from the activity theme.
            You only customize what you need - here the button color. -->
        <item name="colorButtonNormal">@color/blue_500</item>
    </style>

    <style name="ThemeOverlay.AppTheme.Yellow" parent="ThemeOverlay.AppCompat.Light">
        <!-- This worked correctly even with parent="Theme.AppTheme". -->
        <item name="colorButtonNormal">@color/yellow_500</item>
    </style>

</resources>

layout/activity_main.xml (partial)

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:theme="@style/ThemeOverlay.AppTheme.Blue"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:theme="@style/ThemeOverlay.AppTheme.Yellow"/>

EDIT: (oh yeah I forgot) MainActivity.java

public class MainActivity extends AppCompatActivity { ... }

Result

Kitkat Lollipop

Upvotes: 2

kevin-lisnr
kevin-lisnr

Reputation: 43

I found out why the original code didn't work when I thought it should. I was using FragmentActivity from support v4 lib, which doesn't seem to have the new v7 AppCompat features for themes built in. Changing my activity to AppCompatActivity makes the theming work with no changes from what I had.

A More technical explanation here: https://stackoverflow.com/a/29978777/3517023

Upvotes: 1

Related Questions