Arnab Chakraborty
Arnab Chakraborty

Reputation: 7472

How to change textColor of a button from XML in Android?

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

Upvotes: 4

Views: 20984

Answers (4)

Androider
Androider

Reputation: 724

If it's programmatically, you could use:

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

In xml it'll be the same thing.

Upvotes: 1

Nikola Despotoski
Nikola Despotoski

Reputation: 50578

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

Upvotes: 16

ShineDown
ShineDown

Reputation: 1879

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

Upvotes: 0

Ragunath Jawahar
Ragunath Jawahar

Reputation: 19733

<?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 attribute.

Upvotes: 11

Related Questions