Text color selector doesn't work on android 4

170 views Asked by At

I try to apply simple text color selector for all of my buttons. I override default Button style and use it in my application theme (AppTheme). It works as expected on android 5 and up but it doesn't work on android 4. Check out my code.

enter image description here

styles.xml

<resources>

    <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>
        <item name="android:buttonStyle">@style/Button</item>
    </style>

    <style name="Button" parent="Widget.AppCompat.Button">
        <item name="android:textColor">@drawable/btn_text_color_selector</item>
    </style>

</resources>

btn_text_color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorPrimary" android:state_enabled="false"></item>
    <item android:color="@color/colorAccent"></item>
</selector>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Kalės vaikas" />
1

There are 1 answers

2
Lutaaya Huzaifah Idris On

Instead of using btn_text_color_selector.xml, why don't you just add android:textColor="@color/colorPrimary" :

Like this

 <Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
android:textColor="@color/colorPrimary"
        android:layout_height="wrap_content"
        android:text="Kalės vaikas" />

This will maintain all the platforms .