Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 559
Arnab Chakraborty Avatar asked Jul 28 '11 13:07

Arnab Chakraborty


People also ask

How do I change the text color of a button in XML?

To set Android Button text color, we can assign android:textColor XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button text color, we can pass specified color to the method Button. setTextColor(new_color).

How do I change the color of my button on Kotlin?

Change Android Button Background Color Programmatically In this example project, we will change the Button background color programmatically in MainActivity. kt program using Button. setBackgroundColor(color).

How do I change the default button color?

To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.


2 Answers

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"

like image 159
Nikola Despotoski Avatar answered Oct 01 '22 00:10

Nikola Despotoski


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

like image 43
Ragunath Jawahar Avatar answered Sep 30 '22 23:09

Ragunath Jawahar