4

I have a defined Drawable Resource(res/drawable/.xml) which contains defined Color Resources (res/color/.xml). Color resources contains of state selector.

In Android 5+ everything works properly, Color State Selector works properly. But on Android 4 it's not working. Why this is caused ? How to fix it ?

Layout:

<Button
    android:background="@drawable/btn_login"

drawable/btn_login.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <stroke
                android:width="@dimen/login_button_border"
                android:color="@color/btn_login_border" />
            <solid android:color="@color/btn_login_background" />
        </shape>
    </item>
    <item android:bottom="@dimen/login_button_border">
        <shape android:shape="rectangle">
            <stroke
                android:width="@dimen/login_button_border"
                android:color="@color/btn_login_background" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
</layer-list>

color/btn_login_border.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/login_border_highlighted" android:state_pressed="true"/>
    <item android:color="@color/login_border"/>
</selector>

color/btn_login_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/login_button_highlighted" android:state_pressed="true"/>
    <item android:color="@color/login_button"/>
</selector>
Maxian Nicu
  • 2,166
  • 3
  • 17
  • 31

1 Answers1

4

Because of ColorStateList is not supported on pre-Lollipop, maybe you can reorganize shown resources like this:

drawable/btn_login.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/login_button_highlighted" android:state_pressed="true"/>
    <item android:drawable="@drawable/login_button"/>
</selector>

drawable/btn_login_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <stroke
                android:width="@dimen/login_button_border"
                android:color="@color/login_border" />
            <solid android:color="@color/login_button" />
        </shape>
    </item>
    <item android:bottom="@dimen/login_button_border">
        <shape android:shape="rectangle">
            <stroke
                android:width="@dimen/login_button_border"
                android:color="@color/login_button" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
</layer-list>

drawable/btn_login_highlihted.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <stroke
                android:width="@dimen/login_button_border"
                android:color="@color/login_border_highlighted" />
            <solid android:color="@color/login_button_highlighted" />
        </shape>
    </item>
    <item android:bottom="@dimen/login_button_border">
        <shape android:shape="rectangle">
            <stroke
                android:width="@dimen/login_button_border"
                android:color="@color/login_button_highlighted" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
</layer-list>

This will remove the need of using color selectors in this case.

paulina_glab
  • 2,467
  • 2
  • 16
  • 25