setBackgroundColor changes color of more then the button

1.5k Views Asked by At

I'm trying to set color to a button, but when I write:

 button.setBackgroundColor(getResources().getColor(R.color.white));

the button becomes white, but also some space around it (I have couple of buttons in linearLayout, so it looks like one big white button).

Anyone knows how to fix this?

Update: My XML:

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:weightSum="2"
        android:layout_weight="1"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:background="@android:color/white"
            android:id="@+id/button1"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button2"                
            />


    </LinearLayout>

Here the left button looks bigger then the right one because I changed its color

5

There are 5 best solutions below

2
On BEST ANSWER

It's because the default implementation of a button uses a custom drawable as the background and changing the background will override it and lose all the stylings.

Instead what you want to do is to overlay the existing background drawable with a color:

button.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);

You could also find the default style that the button uses, copy it and change the colors there but that would be more work.

2
On

you don't need to write that code in your activity

just in your XML :

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:background="#ffffff"
        android:id="@+id/button1"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button2"                
        />

visit this website for color codes:

http://color-hex.com

1
On

Did you try

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:weightSum="2"

    >
    <Button
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:background="@android:color/white"
        android:id="@+id/button1"
        />
    <Button
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button2"                
        />


</LinearLayout>

In your parrent layout (LinearLayout) you set weightSum is 2 but not contribute for child by using android:layout_weight="1" in childs elements

1
On

Set the height and width of of your button to an specific size not to wrap content if you use a background color.

0
On

You can try in Xamarin

button.Background.SetColorFilter(Android.Graphics.Color.ParseColor("#f00ece"), Android.Graphics.PorterDuff.Mode.Multiply);