Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: change button background programmatically

Tags:

android

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.

like image 227
Federico klez Culloca Avatar asked Nov 27 '22 05:11

Federico klez Culloca


2 Answers

We were trying to implement the tab like functionality using just plain buttons in Android. I was unsuccessful to obtain the right behavior using the XML- must be doing something wrong. For instance, I could get the button color to change to yellow as long as the button was pressed, but it would go back to original color when left.

Finally, I could achieve the desired behavior by using following code in the click event handler for each of the tab buttons. Hope this helps someone having similar issue as me.

// Get Handle for the Tab buttons
Button btnTab1 = (Button) findViewById(R.id.button_tab1);
Button btnTab2 = (Button) findViewById(R.id.button_tab1);

// set the colors correctly
btnTab1.setBackgroundResource(color.lightblue);
btnTab2.setBackgroundResource(color.darkblue);
like image 179
Sandy1978 Avatar answered Dec 07 '22 23:12

Sandy1978


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">

<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"
            android: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"
            android:color="#00ff00" />
        <corners
            android:radius="5dp" />
    </shape>
</item>

like image 36
Levara Avatar answered Dec 07 '22 23:12

Levara