tags:

views:

56

answers:

2

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.

A: 

setBackgroundResource is expecting a drawable, not a color. You want setBackgroundColor.

Also, your colors should have at least 6 digits, #RRGGBB

I.e., for blue: #0000FF

I think what you have might be equivalent to #00000F, which is close to black...

And finally, you should never sleep in the UI thread (or change UI items not in the UI thread). See Painless Threading for a number of different ways of using other threads. I think that postDelayed might be what you are looking for.

Mayra
nope, the buttons turn black and don't change back to their original color. at least in the emulator. when I'll finish refleshing my G1 I'll try on that
klez
See edits for more info
Mayra
+1  A: 

You could use an xml file like one below, to create states for your button.

The info about attributes available is here. You simply copy this xml file to your drawables folder in your project, name it for example custom_button.xml, and reference it in your layout with

android:background="@drawable/custom_button"

Here is xml file...

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"&gt;

<item android:state_pressed="true" >
    <shape android:shape="rectangle">
        <solid
            android:color="#00ff00" />
        <stroke
            android:width="5dp"
            android:color="#ff0000"
            android:dashWidth="3dp"
            android:dashGap="2dp" />
    </shape>
</item>

<item android:state_focused="true" >
    <shape>
        <gradient
            android:endColor="#ffffff"
            android:centerColor="#ffffff"
            android:startColor="#ffffff"
            android:angle="270" />
        <stroke
            android:width="3dp"
            color="#00ff00" />
        <corners
            android:radius="5dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<item>        
    <shape>
        <gradient
            android:endColor="#ffffff"
            android:centerColor="#ffffff"
            android:startColor="#ffffff"
            android:angle="270" />
        <stroke
            android:width="5dp"
            color="#00ff00" />
        <corners
            android:radius="5dp" />
    </shape>
</item>

Levara
At the moment I'm a bit busy. As soon as I'll try it I'll let you know. For now thanks.
klez
I had to adapt it a bit but works perfectly! Thanks a lot
klez