Saint Robson
Saint Robson

Reputation: 5525

Change Background Color From Drawable Button

I have this round_button.xml file :

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

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimary"></solid>
            <corners android:radius="3dp"></corners>
        </shape>
    </item>

</selector>

And I use that drawable for my two buttons :

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/Button_Register"
            android:background="@drawable/round_button"
            android:textColor="@color/formLabelTextColor"
            android:text="@string/loginform_buttonregister" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/Button_Login"
            android:background="@drawable/round_button"
            android:textColor="@color/komWhite"
            android:text="@string/loginform_buttonlogin" />

my question is...

how to change Button_Register background color in java? I tried to use this :

    Button button_register = (Button) findViewById(R.id.Button_Register);
    button_register.setBackgroundColor(R.color.formBackgroundColor);

and it removes the drawable shape. I just want to change the color for that particular button. but how?

thank you.

Upvotes: 0

Views: 8421

Answers (5)

Eli
Eli

Reputation: 25

Based in the answer of sasikumar this is the code of a rectangle that you can use for a button, pay attention to the:

<solid android:color="@color/colorWhite"></solid>

The full code of the drawable is:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/button_square">
    <stroke android:width="1dp" android:color="#3f3f44" />
    <padding android:left="2dp"
    <solid android:color="@color/colorWhite"></solid>
    android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="10dp" />
</shape>

And this is how i implement it:

 <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginEnd="5dp"
    android:background="@drawable/buttonsquare"
    android:text="Button text"
    android:layout_marginStart="5dp">
 </Button>

Upvotes: 0

Pramit Patel
Pramit Patel

Reputation: 59

You can do this by creating drawable at runtime with color you need. Following code will help you to make drawable programmatically -

Button button = (Button) findViewById(R.id.btn_login);
final GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadius(10);
gradientDrawable.setColor(Color.BLUE);
button.setBackground(gradientDrawable);

Hope it helps you.

Upvotes: 0

TibiG
TibiG

Reputation: 849

public static void setDrawableFilterColor(Context context, int colorResource, Drawable drawable) {
    //noinspection ResourceType
    int filterColor = Color.parseColor(context.getResources().getString(colorResource));
    drawable.setColorFilter(new PorterDuffColorFilter(filterColor, PorterDuff.Mode.MULTIPLY));
}

and call it with

setDrawableFilterColor(mContext, R.color.colorPrimary, button_register.getBackground())

Upvotes: 5

Junaid Hafeez
Junaid Hafeez

Reputation: 1616

you can simply change the background color of your xml drawable with GradientDrawable something like this

GradientDrawable gradientDrawable = (GradientDrawable)button_register.getBackground();
gradientDrawable.setColor(R.color.formBackgroundColor);

Upvotes: 4

Sasi Kumar
Sasi Kumar

Reputation: 13278

set drawable color on your shape property in selector file like this

<item android:drawable="@color/yourcolor">

complete code

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/black">
    <shape android:shape="rectangle">
        <solid android:color="@color/colorPrimary"></solid>
        <corners android:radius="3dp"></corners>      
    </shape>
</item>
</selector>

Upvotes: 2

Related Questions