sandeep
sandeep

Reputation: 947

How to change font color of a textView

I want to use the below method of textView to change the font color. What format can I use to specify the color?

public void setTextColor (int color)

Sets the text color for all the states (normal, selected, focused) to be this color.

I've tried this: text.setTextColor(#FF0000)

But it's not a valid syntax.

Upvotes: 2

Views: 9233

Answers (5)

Mohit Kanada
Mohit Kanada

Reputation: 15974

For set that color u have many options... 1 textview.setTextColor(Color.rgb(012,255,0));

2 first define the color in values... values.xml is define in your project under src folder... U can add your color as below

<?xml version="1.0" encoding="utf-8"?>
  <resources>
    <string name="hello">Hello World</string>
    <string name="app_name">XYZ</string>
    <color name="SkyBlue">#56A5EC</color>
    <color name="Green">#347C17</color>
  </resources>

after define color in values simply use as below...

textview.setTextColor(R.Color.SkyBlue);

Upvotes: 3

Paresh Mayani
Paresh Mayani

Reputation: 128428

I am totally agree with answers of Mohit Kanada and Houcine.

But let me add something here, you can also use some built-in resources provided by android itself, just refer below image: enter image description here

Upvotes: 1

Houcine
Houcine

Reputation: 24181

TextView.setTextColor(Color.BLUE);

TextView.setTextColor(Color.RED);

Or this :

textView.setTextColor(Color.rgb(255,0,0)); // rgb( red , green , blue ) ;

Upvotes: 4

Stefan H Singer
Stefan H Singer

Reputation: 5504

It expects a 32bit color value. You can create one using the Color class: http://developer.android.com/reference/android/graphics/Color.html#rgb(int, int, int)

edit: you can also use 0xAARRGGBB (in your case, that would be 0x00FF0000 for pure red).

Upvotes: 2

Dimse
Dimse

Reputation: 1506

You should use the color class. Read about it here: http://developer.android.com/reference/android/graphics/Color.html

Upvotes: 2

Related Questions