miércoles, 8 de enero de 2014

Android: Custom "toggle button"

We will use an ImageView as a toggle button. Having two states, a normal one and one when its pressed and an action when it is pressed.

<ImageView
        ...

        android:clickable="true"
        android:src="@drawable/favorite_button_selector"
        android:onClick="favoriteClicked" />

res/drawable

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item android:drawable="@drawable/favorite_active" android:state_selected="true"/>
    <item android:drawable="@drawable/favorite_inactive" android:state_selected="false"/>

</selector>

To make this work though we need to explicitly call setSelected(boolean) somewhere in your class. The best place to do so is probably in the onClick handler (favoriteClicked in this case).

No hay comentarios:

Publicar un comentario