Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change text color in shape on touch of button

How to change color text in button when the user touches this button?

Here is my shape.xml code:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >


 <item android:state_pressed="true" >
<shape android:shape="rectangle">
  <gradient android:startColor="#ffcc33" 
    android:endColor="#ffcc33"
    android:angle="270" />
  <corners android:radius="4dp" />
  <stroke android:width="2px" android:color="#FFFFFF" />
</shape>
</item>

  <item android:state_focused="true" >
<shape android:shape="rectangle">
  <gradient android:startColor="#ffcc33" 
    android:endColor="#ffcc33"
    android:angle="270" />
  <corners android:radius="4dp" />
  <stroke android:width="2px" android:color="#FFFFFF" />
</shape>
</item>

  <item >
<shape android:shape="rectangle">
  <gradient android:startColor="#333333" 
    android:endColor="#333333"
    android:angle="270" />
  <corners android:radius="4dp" />
  <stroke android:width="2px" android:color="#FFFFFF" />
</shape>
</item>

</selector>
like image 479
Mateusz Ryndak Avatar asked Jan 30 '26 08:01

Mateusz Ryndak


1 Answers

Use color selector, something like this

src/color/button_text.xml

 <?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" /> 
     <item android:state_focused="true" android:color="#000000" />
     <item android:color="#FFFFFF" />
 </selector>

Then in the button you do this

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/text"
   android:textColor="@color/button_text" />
like image 59
Olayinka Avatar answered Feb 01 '26 20:02

Olayinka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!