T_Bacon
T_Bacon

Reputation: 404

Color State List not working for shape stroke - Android

I'm trying to get the stroke of my shape to change colour based on state (of an edittext box), but it always shows as the default colour. I've tried moving into /drawable from /color to no effect, still doesn't work. For some reason, state_pressed and state_focused seem to be ignored... Any ideas?

state selector currently in /color:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/LIGHTBLUE"/>   <!-- pressed -->
    <item android:state_focused="true" android:color="@color/ORANGE"/>   <!-- focused -->
    <item android:color="@color/GREY"/>     <!-- default -->
</selector>

drawable:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edittext_shape" >

    <!-- <solid android:color="#ffffff" /> -->

    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#FFFFFFFF"
        android:angle="270"
        android:centerX="0.25" />
    <corners
        android:bottomLeftRadius="6dp"
        android:bottomRightRadius="6dp"
        android:topLeftRadius="6dp"
        android:topRightRadius="6dp" />
    <padding
        android:left="7dp"
        android:right="7dp"
        android:bottom="5dp"
        android:top="5dp" />

    <stroke 
        android:width="7dp"
        android:color="@color/edit_text_color_state" />
</shape>

Upvotes: 2

Views: 1333

Answers (1)

T_Bacon
T_Bacon

Reputation: 404

So after some messing about and trying everything under the sun, I finally got this to work by creating the shape within the selector:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape>
            <solid android:color="#ffffff" />
            <corners
                android:bottomLeftRadius="2dp"
                android:bottomRightRadius="2dp"
                android:topLeftRadius="2dp"
                android:topRightRadius="2dp" />
            <padding
                android:left="7dp"
                android:right="7dp"
                android:bottom="5dp"
                android:top="5dp" />

            <stroke 
                android:width="2dp"
                android:color="@color/ORANGE" />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape>
            <solid android:color="#ffffff" />
            <corners
                android:bottomLeftRadius="2dp"
                android:bottomRightRadius="2dp"
                android:topLeftRadius="2dp"
                android:topRightRadius="2dp" />
            <padding
                android:left="7dp"
                android:right="7dp"
                android:bottom="5dp"
                android:top="5dp" />
            <stroke 
                android:width="2dp"
                android:color="@color/LIGHTBLUE" />
        </shape>
    </item>    

    <item>
        <shape>
            <solid android:color="#ffffff" />
            <corners
                android:bottomLeftRadius="2dp"
                android:bottomRightRadius="2dp"
                android:topLeftRadius="2dp"
                android:topRightRadius="2dp" />
            <padding
                android:left="7dp"
                android:right="7dp"
                android:bottom="5dp"
                android:top="5dp" />

            <stroke 
                android:width="2dp"
                android:color="@color/GREY" />
        </shape>
    </item>
</selector>

It means repeating code unnecessarily as all I wanted to change was the stroke colour, but it works.

Upvotes: 1

Related Questions