Skip to content Skip to sidebar Skip to footer

How To Set Button Selection Color Along With Rounded Corners In Android?

I want to set a rounded corner for a button in android along with changing the button color on when selected. I am doing the following things. drawable/push_button.xml

Solution 1:

I have found answer for my question with few trial & error attempts.

Here is the solution.

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:state_pressed="true"><shape  ><solidandroid:color="@color/green"/><cornersandroid:radius="7dp"/></shape></item><itemandroid:state_focused="true" ><shapexmlns:android="http://schemas.android.com/apk/res/android" ><solidandroid:color="@color/green"/><cornersandroid:radius="7dp"/></shape></item><itemandroid:state_focused="false" ><shapexmlns:android="http://schemas.android.com/apk/res/android" ><solidandroid:color="@color/red"/><cornersandroid:radius="7dp"/></shape></item><itemandroid:state_pressed="false" ><shapexmlns:android="http://schemas.android.com/apk/res/android" ><solidandroid:color="@color/red"/><cornersandroid:radius="7dp"
    /></shape></item></selector>

Solution 2:

What I did was defined the shape and specify the dp of each corner.

<shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"android:padding="90dp"><solidandroid:color="#FFFFFF"/><padding /><cornersandroid:bottomRightRadius="15dp"android:bottomLeftRadius="15dp"android:topLeftRadius="15dp"android:topRightRadius="15dp"/></shape>

If you increase the dp in each corner it will make the button more rounded.

Solution 3:

I have been successful at doing exactly what you're describing. I did the following:

First, I created a new class that extends Button. In this class, I created a method called setState():

publicvoidsetState(int s){
    if (s > 0 && s < 4 &&)
    {
        this.state = s;
        switch (state)
        {
            case1:
                setBackgroundDrawable (def_gray);
                break;

            case2:
                setBackgroundDrawable (lt_gray);
                break;

            case3:
                setBackgroundDrawable (black);
        }
    }
}

The three background drawables you see above are XML files describing the button look. They are mostly the same, but vary in the color set for each one. The default gray button is described like this:

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><item><shape><gradientandroid:startColor="#333333"android:endColor="#333333" /><strokeandroid:width="2dp"android:color="@android:color/white" /><cornersandroid:radius="5dp" /><paddingandroid:left="2dp"android:top="2dp"android:right="2dp"android:bottom="2dp" /></shape></item></selector>

As far as I know, that XML format is expecting to be used to configure a color gradient on the button face. Since that wasn't what I wanted, I set both color values the same and the background color is consistent. You may need to experiment a bit with color values, but it looks like you've already got a handle on that.

Post a Comment for "How To Set Button Selection Color Along With Rounded Corners In Android?"