Reputation: 2229
I have a button in my activity on which I want the following behaviour
The button should have a selected color border When the button is pressed, that selected color should fill the whole button
For eg if I choose orange, the button should have a default orange background and when the button is pressed, the button should be filled with orange.
So I made this custom drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_selected="true">
<shape>
<solid android:color="@color/colorAccent" />
<corners android:radius="30dp" />
<stroke android:width="2dp" android:color="@color/colorAccent" />
</shape>
</item>
<item>
<shape>
<corners android:radius="30dp" />
<stroke android:width="2dp" android:color="@color/colorAccent" />
</shape>
</item>
</selector>
I applied this as my background to the button and I got the default behavior right. But when the button is pressed, I observe no change.
How do I make it work?
Upvotes: 0
Views: 472
Reputation: 58934
If you put two property android:state_pressed="true" android:state_selected="true"
then it will work like and(&)
operator. Correct way is to set individual <item/>
for all behaviour
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="30dp" />
<solid android:color="@color/colorAccent" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle">
<corners android:radius="30dp" />
<solid android:color="@color/colorAccent" />
</shape>
</item>
<item android:state_selected="true">
<shape android:shape="rectangle">
<corners android:radius="30dp" />
<solid android:color="@color/colorAccent" />
</shape>
</item>
<item android:state_focused="false" android:state_pressed="false">
<shape>
<corners android:radius="30dp" />
<stroke android:width="2dp" android:color="@color/colorAccent" />
</shape>
</item>
</selector>
Upvotes: 1