0

I have a pretty simple problem. I'm creating several buttons with a method in an activity. However the text colour just remains in the standard colour (grey). I defined the following in my method:

Button b = new Button(this);
b.setTextColor(R.color.red);
b.setText("Some text");

Is anyone aware of this problem and can help me out? By googling I read sth. about spannable. However it seems that this isn't working with the text of buttons.

user1335772
  • 277
  • 1
  • 3
  • 10
  • Several ways to skin this cat - nice answer here: http://stackoverflow.com/questions/4602902/how-to-set-text-color-of-textview-by-coding – SEngstrom Apr 20 '12 at 20:42

2 Answers2

3

R.color.red is a resource identifier (in Android they use Integers). You need to use that code like so:

Resources res = getResources();
int red = res.getColor(R.color.red);

Button b = new Button(this);
b.setTextColor(red);
b.setText("Some text");
Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
2

You need to call getResources() before you tell it what color you want.

getResources().getColor(color)
adneal
  • 30,484
  • 10
  • 122
  • 151