40

How do I change the text color of a Button?

hello_there_andy
  • 2,039
  • 2
  • 21
  • 51
Jason Ching
  • 1,037
  • 4
  • 19
  • 27

10 Answers10

97

try this:

button.setTextColor(getApplication().getResources().getColor(R.color.red)); //TAKE DEFAULT COLOR

or

button.setTextColor(0xff0000); //SET CUSTOM COLOR 

or

button.setTextColor(Color.parseColor("#ff0000")); 

and in xml :

<Button android:id="@+id/mybtn" 
        android:text="text textx "  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"  
        android:textStyle="bold" 
        android:textColor="#ff0000" />  <-- SET TEXT COLOR HERE -->
Nate
  • 31,017
  • 13
  • 83
  • 207
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • I know this post is two years back, just wanted to highlight that button.setTextColor(int color) uses the static values from the Color class, instead of defining your own hexadecimal color. E.g. Color.GREEN – Uknight Aug 02 '14 at 02:07
  • 4
    I am using your post `android:textColor="#003EFF"` to change the text-color of button, its working fine when I see on the `.xml` page but when I run emulator and check on it, its not change the button:text color. Why? – user88 Nov 20 '14 at 07:21
  • ```button.setTextColor(0xFF0000); ``` does not work on API 19 – Aaron Sep 02 '18 at 14:59
9

Use the android:textColor property.

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World"
    android:textColor="@android:color/white" />
James Cross
  • 7,799
  • 8
  • 24
  • 30
6
button.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.red));

this work too

Dmitry
  • 6,716
  • 14
  • 37
  • 39
Andreas
  • 255
  • 3
  • 16
3

Use: android:textColor="#FFFFFF" on the xml configuration,

or on the activity itself by calling

button.setTextColor(0xFFFFFF);

(FFFFFF is the color white).

For more color codes: here

Nuno Gonçalves
  • 6,202
  • 7
  • 46
  • 66
  • Same comment as I added to ρяσѕρєя K's answer, the button.setTextColor(int color) uses a static value from the Color class. His next line is the correct one: button.setTextColor(Color.parseColor("#ff0000")); – Uknight Aug 02 '14 at 02:08
1

You can use the android textColor for foreground and for background color of button, text view or any other element see code example

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:background="#ffb6c1"
        android:textColor="#fff"
        />

any hexadecimal color code can be written for making interactive view.

NomanJaved
  • 1,324
  • 17
  • 32
1

change button text color programmatically

button.setTextColor(getResources().getColor(R.color.colorWhite));
Abdul Basit Rishi
  • 2,268
  • 24
  • 30
0

An easy way to do this is by defining the color you want in res/values/colors.xml in this way:

<color name="colorCyan">#00BCD4</color>

and the button should look this way:

<Button
    android:id="@+id/m_button"
    android:text="MY BUTTON"
    android:textColor="@color/colorAccent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorCyan"/>
0

Here's an approach with slightly less code that uses the implied Context of the current Activity.

button.setTextColor(getColor(R.color.colorPrimary));

I have not tested this with all API targets, but it is working for 28.

EmpathicSage
  • 1,265
  • 8
  • 11
0

You can use:

button.setTextColor("green");

or

button.setTextColor(colorcode);

Javasick
  • 2,753
  • 1
  • 23
  • 35
DragonS
  • 31
  • 1
  • 7
0

The format is AARRGGBB, so FF0000 is a transparent red. Use FFFF0000 instead.