Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android button selector background color

I'm trying to implement button with configurable colors for default and pressed states and rounded corners. Here is a solution I've found on SO and seems it work fine on my real test devices:

enter image description here

    GradientDrawable gradientStateNormal = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]
            {buttonBaseBgColor, buttonBaseBgColor});
    gradientStateNormal.setShape(GradientDrawable.RECTANGLE);
    gradientStateNormal.setCornerRadius(cornerRadiusPX);

    GradientDrawable gradientStatePressed = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]
            {colorLight, colorLight});
    gradientStatePressed.setShape(GradientDrawable.RECTANGLE);
    gradientStatePressed.setCornerRadius(cornerRadiusPX);

    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] {android.R.attr.state_pressed},gradientStatePressed);
    states.addState(StateSet.WILD_CARD, gradientStateNormal);
    signUpButton.setBackground(states);

But both Android 4.3 and 4.4 Emulators draw such black corners after press on button:

enter image description here

Could you please explain what's wrong here and could such case appear on the real device, not only emulator?

like image 354
Sergii Avatar asked Aug 04 '26 06:08

Sergii


1 Answers

You can do that by using the xml itself. Make an xml file, for eg:shape.xml as given below:

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

<gradient android:angle="180"
    android:startColor="#FFFF00"
    android:centerColor="#FFFF00"
    android:endColor="#FFFF00"/>

<corners 
    android:radius="20dp"/>

<padding
    android:bottom="10dp"
    android:left="10dp"
    android:right="10dp"
    android:top="10dp" />

<stroke
    android:width="2dp"
    android:color="#000000" />

</shape>

Now use this xml as the background for the button in your main.xml like this:

 android:background="@drawable/shape
like image 196
Maverick Avatar answered Aug 06 '26 22:08

Maverick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!