There are times that when changing a button’s background color, we also want to change the text’s color. There is a method setTextColor(int color) specifically for this purpose. Seems pretty straightforward enough, but it took me a few tries to get it right the first time I tried using it. Documenting it here so that I wouldn’t forget.

Here’s a screenshot of my button with its default settings.

What I’m trying to do is have the color of the text change to a shade of blue once the button is pressed. But look what I got.

That is no shade of blue! Far from it! I tried changing the alpha value of the #AARRGGBB value, but I still end up with the same gray shade. It turns out that you cannot pass a direct R.* id to the setTextColor() method.

Here’s how I finally managed to make it work:

int newColor = getResources().getColor(R.color.button_new_color);

And tada!