How to change textColor buttons from XML in Android?

I use selector to change the background of the button in different states (focused, pressed, normal). Is there a way to change the color of the text? I want to provide different text colors for different button states, but I want to do this from xml. Is it possible?

+4
source share
4 answers

Yes, it can be done. You just do the same thing you do for the button. Then assign it to android:textColor="@drawable/yourselector"

+12
source
 <?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> 

Try combining the above with the android:drawable .

+10
source

If it is programmatic, you can use:

 Button button = findViewById(R.id.yourbutton); button.setTextColor(Color.yourcolor); 

In xml it will be the same thing.

+1
source

Use android:color="#ff0000" in the selector for focused and another color for the default state. Then in the xml of the android:textColor="@drawable/yourselector" button android:textColor="@drawable/yourselector"

0
source

All Articles