SoFunction
Updated on 2025-03-04

The color change effect of the font after clicking button in Android

The button click effect is undoubtedly very simple, so I was so lazy that when the UI told me that when clicking, the button font color should also change with the background, I told him to ask him to cut two pictures. Later, I thought about it, it was really unreliable, so I learned about how to add the button click font color change effect.

1. First, you need to add a few color values ​​you need to in your color file. Note that the difference is whether the general color tag is the drawable tag, just like this:

<drawable name="color_red">#fffa3d39</drawable> 
<drawable name="color_green">#ff00adba</drawable> 
<drawable name="color_gray">#fff4f4f8</drawable>

2. Then you need to define a drawable file, which is similar to a normal selector file. The only difference is to turn the drawable field into a color and introduce the color you just defined, just like this

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:andro> 
<item android:state_focused="false" android:state_enabled="true" android:state_pressed="false" 
android:color="@drawable/color_red" /> 
<item android:state_enabled="false" android:color="@drawable/color_gray" /> 
<item android:state_pressed="true" android:color="@drawable/color_green" /> 
<item android:state_focused="true" android:color="@drawable/color_red" /> 
</selector>

3. Finally, set the drawable file written in the second step to the textColor option in your layout file.

The above is the color change effect of the button click font in Android introduced to you by the editor. I hope it will be helpful to everyone!