jueves, 12 de diciembre de 2013

Android: TextView with hover / no-hover colors

TextColor

In this example I wanted to have my text change its color if the user was hovering over it.

Steps:

1 - Create a Selector resource file and place it in the res/drawables folder. Each item associates a color with a state. Android will make the drawable change for you.
2 - Create a TextView in the desired layout and set its "textColor" to the Selector resource file name.

Step 1:

res/color/enter_as_guest_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/white" 
        android:state_pressed="true"/>
    <item android:color="@color/orange" 
        android:state_enabled="true"/>
</selector>

Step 2:

<TextView
        ...
        android:textColor="@color/enter_as_guest_selector"
        ... />


Background

res/values/colors.xml

<resources>
    <color name="white">#FFFFFF</drawable>
    <color  name="gray">#eaeaea</drawable>
</resources>

res/drawable/selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/gray" />
    <item android:drawable="@drawable/white" />

</selector>

No hay comentarios:

Publicar un comentario