1

I've looked around for a proper way of handling the background color of Buttons in android 5.0 and the only solution I could find was to define a style for the Button in values-21/styles.xml :

<item name="android:colorButtonNormal">#2196f3</item>

Is this the only way of coloring a button, while preserving both its design and the ripple effect ? If yes, It would imply that I have to define a custom theme for each Button which has a different color, really ???

N.B : My question doesn't relate to backwards compatibility and AppCompat, which has already been discussed a lot.

  • See http://stackoverflow.com/questions/27735890/lollipops-backgroundtint-has-no-effect-on-a-button – alanv Jan 14 '15 at 00:33
  • This doesn't answer the question, as it would imply to set the background color first, in order to get the backgroundTint to work. As mentioned in my question, I'm looking for a way to preserve the material aspect of my button.So the only solution I'm left with is to play with different styles and set the theme attribute on the button ? – Standaa - Remember Monica Jan 14 '15 at 09:33
  • 1
    Basically yes. There's a bug in the current version of Lollipop whereby GradientDrawable doesn't respect tint, so the only option is to create your own background or use android:theme on the Button to override the value of the colorButtonNormal attribute. – alanv Jan 14 '15 at 18:06
  • Thanks Alanv. I decided to follow the multi-style way, works well and enables me to keep the material aspect. – Standaa - Remember Monica Jan 15 '15 at 09:03
  • Can people comment on the downvotes ? – Standaa - Remember Monica Jun 11 '15 at 17:38
  • I also faced the case when I need raised buttons with different colors and I ended up with http://stackoverflow.com/a/32542304/746347. – mixel Sep 15 '15 at 13:04

2 Answers2

0

You can use the tint option to change coloring that way, instead of making a seperate theme for each button, you would just have each tinted a different color. See here: Material Design Drawables

Dest
  • 41
  • 1
  • 8
  • I wouldn't have asked the question if the answer was so trivial. Setting background tint doesn't change anything unless you set a background color, and therefore loose the material formatting. – Standaa - Remember Monica Jan 14 '15 at 09:26
0

Yes, you have to define a separate theme with colorButtonNormal for each color. You can set theme to your Button as following:

<style name="ColorButonButton" parent="Theme.AppCompat.Light">  
    <item name="colorButtonNormal">@color/newColor</item>
</style>

<Button  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Text"
    android:theme="@style/ColorButonButton"/>
Sharjeel
  • 15,588
  • 14
  • 58
  • 89