3

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

trejder
  • 17,148
  • 27
  • 124
  • 216
sagicoh
  • 105
  • 11

5 Answers5

3

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.

Simas
  • 43,548
  • 10
  • 88
  • 116
  • Can you tell me the style's name or how to find it? I tried using the color filter - I think the MULTIPLY Mode isnt working – sagicoh Jun 21 '15 at 09:49
  • @user3142471 I've tried it and it works perfectly fine for the standard Button on API 22. The style depends on the theme you're using but you can probably find it via the attribute `android.R.style.Widget_Button`. Also [this question](http://stackoverflow.com/q/1521640/3249477) might have more information. – Simas Jun 21 '15 at 09:54
2

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

  • I need to create the buttons in the code and still in the xml writing `android:background="#ffffff"` didnt change anything – sagicoh Jun 21 '15 at 08:13
  • 1
    no it changes color bro.just try my code its simple and working fine. also you can create color.XML file and put color codes in there –  Jun 21 '15 at 08:25
1

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

  • Also you can set some layout margine to give ieach some distance from others –  Jun 21 '15 at 11:21
0

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

ThaiPD
  • 3,503
  • 3
  • 30
  • 48
  • I understand as you want to have 2 button with same size with above code, right? otherwise could you please tell me what is your expected? – ThaiPD Jun 21 '15 at 09:45
0

You can try in Xamarin

button.Background.SetColorFilter(Android.Graphics.Color.ParseColor("#f00ece"), Android.Graphics.PorterDuff.Mode.Multiply);
Sheriff
  • 738
  • 10
  • 20