I have this resource file for colors
<resources>
<color name="CLR_BLUE">#00f</color>
<color name="CLR_RED">#f00</color>
<color name="CLR_GREEN">#0f0</color>
<color name="CLR_YELLOW">#ff0</color>
<color name="CLR_BLUE_DARK">#00a</color>
<color name="CLR_RED_DARK">#a00</color>
<color name="CLR_GREEN_DARK">#0a0</color>
<color name="CLR_YELLOW_DARK">#aa0</color>
</resources>
And this method that gets called when the user clicks one of four colored Buttons
private void changeBackground(Button pressedBtn)
{
int oldColor = 0;
int newColor = 0;
if(pressedBtn == greenBtn) {
oldColor = R.color.CLR_GREEN;
newColor = R.color.CLR_GREEN_DARK;
}
else if (pressedBtn == redBtn) {
oldColor = R.color.CLR_RED;
newColor = R.color.CLR_RED_DARK;
}
else if (pressedBtn == yellowBtn) {
oldColor = R.color.CLR_YELLOW;
newColor = R.color.CLR_YELLOW_DARK;
}
else if (pressedBtn == blueBtn) {
oldColor = R.color.CLR_BLUE;
newColor = R.color.CLR_BLUE_DARK;
}
else return;
pressedBtn.setBackgroundResource(newColor);
SystemClock.sleep(500);
pressedBtn.setBackgroundResource(oldColor);
}
The problem is the color of the Button doesn't change when pressed.
I stepped with a debugger and it actually reaches the right points in the method, so that's not an issue. I think the problem is in pressedBtn.setBackgroundResource(newColor)
but I can't understand why.
PS: Anyway, if you have a better solution to change a button color when pressed and, after a half second, change back to original color, let me know.