Android: change button background programmatically
<p>I have this resource file for colors
</p>

<pre><code>&lt;resources&gt;
    &lt;color name="CLR_BLUE"&gt;#00f&lt;/color&gt;
    &lt;color name="CLR_RED"&gt;#f00&lt;/color&gt;
    &lt;color name="CLR_GREEN"&gt;#0f0&lt;/color&gt;
    &lt;color name="CLR_YELLOW"&gt;#ff0&lt;/color&gt;
    &lt;color name="CLR_BLUE_DARK"&gt;#00a&lt;/color&gt;
    &lt;color name="CLR_RED_DARK"&gt;#a00&lt;/color&gt;
    &lt;color name="CLR_GREEN_DARK"&gt;#0a0&lt;/color&gt;
    &lt;color name="CLR_YELLOW_DARK"&gt;#aa0&lt;/color&gt;
&lt;/resources&gt;
</code></pre>

<p>And this method that gets called when the user clicks one of four colored <code>Buttons</code></p>

<pre><code>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);

}
</code></pre>

<p>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 <code>pressedBtn.setBackgroundResource(newColor)</code> but I can't understand why.</p>

<p>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.</p>
