Android SetTextColor From Colors

When setting the colors for e.g. a TextView you can use the settextcolor from either a color or resource.

void android.widget.TextView.setTextColor(int color)
public void setTextColor (int color)
Sets the text color for all the states (normal, selected, focused) to be this color.

There are several ways to do this. To set the colors from your resource you will have to add getResources() like this where text is the TextView.

text.setTextColor(getResources().getColor(R.color.errorColor));

To set a general color you can do this:

text.setTextColor(Color.RED);

Parse the color from a hash:

text.setTextColor(Color.parseColor("#FFFFFF"));

You can also use a RGB color with or without alpha:

text.setTextColor(Color.rgb(200,0,0)); text.setTextColor(Color.argb(0,200,0,0));
Posted in Android Tagged with: , ,