change button text color when pressed

skinnybrit51 picture skinnybrit51 · Feb 17, 2012 · Viewed 51.6k times · Source

I have made my button transparent so I would like to have the button text color change when the button is pressed. Is it possible to do this using just xml files?

Answer

damson picture damson · Feb 17, 2012

Yes, you can do it like that:

layout/main_layout.xml:

.....
    <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="bonjour !"
      android:textColor="@color/button_text_color"
    />
.....

color/button_text_color.xml:

   <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:color="#c0c0c0" android:state_pressed="true"/>
     <item android:color="#ffffff"/>
   </selector>