0

I have created a selector:

btn_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- pressed -->
    <item android:drawable="@drawable/btn_bg_pressed" android:state_pressed="true" />

    <!-- focused -->
    <item android:drawable="@drawable/btn_bg_focused" android:state_focused="true" />

    <!-- default -->
    <item android:drawable="@drawable/btn_bg_default" />
</selector>

btn_bg_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@color/btn_pressed"/>
</shape>

btn_bg_focused.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@color/reality_fix_yellow"/>
</shape>

btn_bg_default.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="2dp"
        android:color="@color/btn_default_border_color" />
</shape>

I want to change the text color the button when the focus changes. I've tried adding android:color="color_code" and android:textColor="color_code" but failed. Please help me on How to change the text color of a button with selector?

Anas Azeem
  • 2,820
  • 3
  • 24
  • 37

3 Answers3

3

Define this way :

Create button_selector.xml file in res/color directory

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:color="#000000" /> <!-- pressed -->
     <item android:state_focused="true"
           android:color="#000000" /> <!-- focused -->
     <item android:color="#FFFFFF" /> <!-- default -->
 </selector>

and in Button property define this way.

<Button
    android:TextColor="@color/button_selector"
/>
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
2

There's a trick where you can reuse your background drawable resource to change color for your text.

In your layout.xml, declare your button background and textColor in the following way:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/btn_bg"
    android:textColor="@drawable/btn_bg"
    android:text="Hello"  />

Now, in your drawable/btn_bg.xml, define android:color="@color/your_color_code" as the item attribute:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- pressed -->
    <item android:color="@color/md_green_500" android:drawable="@drawable/btn_bg_pressed" android:state_pressed="true" />

    <!-- focused -->
    <item android:color="@color/md_red_500" android:drawable="@drawable/btn_bg_focused" android:state_focused="true" />

    <!-- default -->
    <item android:color="@color/md_blue_500" android:drawable="@drawable/btn_bg_default" />
</selector>

You should be able to see background and text color change for your button.

You Qi
  • 8,353
  • 8
  • 50
  • 68
0

You can acheive this on your own TextView class that extends the Android TextView class and override the onTouchEvent(MotionEvent event)

You can then modify the instances text color based on the MotionEvent passed.

For example:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
       // Change color
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
       // Change it back
    }
    return super.onTouchEvent(event);
}
Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60