Issue
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
Solution
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.
Answered By - Simas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.