avp::ptr weblog

Everything that is in our mind.

Archive for March, 2013

Android button states understood

without comments

After a lot of research, trial and error I finally understood how I can specify four different button states via XML. The important detail is that you need to add both state parameters in every item. In the following example those are android:state_checked and android:state_pressed which can be combined to four different states.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--
finger is on while button is up
-->
<item android:state_checked="false" android:state_pressed="true"
android:drawable="@drawable/ic_btn_medium_grey"/>
<!--
finger is gone, button is up, default
-->
<item android:state_checked="false" android:state_pressed="false"
android:drawable="@drawable/ic_btn_light_grey"/>
<!--
finger is gone, button is down
-->
<item android:state_checked="true" android:state_pressed="false"
android:drawable="@drawable/ic_btn_dark_grey"/>
<!--
finger is on while button is down
-->
<item android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/ic_btn_deep_dark_grey"/>
</selector>

If you are unsure why you want to have four states for a single button just imagine the button can stay in its checked state.

Written by weblogger

March 21st, 2013 at 7:11 pm

Posted in Uncategorized

Tagged with , , ,