Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use android:background with button from the new material components

Tags:

I'm using the new material components com.google.android.material:material with android x but I can't set a custom background to the button.

I know that I can use app:backgroundTint to change the color

but the default background has some padding that I want to get rid of, and the old way of using android:background to set my own background but this is no longer working.

I looked at the docs but can't find any mention to this change.

like image 788
humazed Avatar asked Oct 05 '18 20:10

humazed


People also ask

How to set background in Material button?

If you want to change the background color you can use the app:backgroundTint attribute or you can override some theme attributes from a default style then you can use new materialThemeOverlay attribute.

How change material button background color in android programmatically?

I try change color to MaterialButton with this code: var materialButton = findViewByid(R.id....) as MaterialButton materialButton. setBackgroundColor( ContextCompat. getColor(this@MyActivity, R.


2 Answers

In the Material Components Library, the MaterialButton has a default style with insetBottom and insetTop with a value of 6dp.

You can change it using:

  <com.google.android.material.button.MaterialButton       android:insetTop="0dp"       android:insetBottom="0dp"       ../> 

enter image description here

If you want to change the background color you can use the app:backgroundTint attribute or you can override some theme attributes from a default style then you can use new materialThemeOverlay attribute.

In your case you can do something like:

<style name="MtButtonStyle"  parent="Widget.MaterialComponents.Button">    <item name=“materialThemeOverlay”>@style/GreenButtonThemeOverlay</item> </style>  <style name="GreenButtonThemeOverlay">   <item name="colorPrimary">@color/green</item> </style> 

Finally starting with the version 1.2.0-alpha06 you can use the android:background attribute in the MaterialButton.

<MaterialButton     app:backgroundTint="@null"     android:background="@drawable/button_drawable"     ... /> 
like image 191
Gabriele Mariotti Avatar answered Sep 28 '22 06:09

Gabriele Mariotti


The documentation for the MaterialButton class says:

Do not use the android:background attribute. MaterialButton manages its own background drawable, and setting a new background means MaterialButton can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed, MaterialButton cannot guarantee well-defined behavior.

However, the GitHub readme says:

Note: MaterialButton is visually different from Button and AppCompatButton. One of the main differences is that AppCompatButton has a 4dp inset on the left and right sides, whereas MaterialButton does not.

This mentions only left/right inset, but the Attributes section of the readme shows that all four insets are supported:

enter image description here

So you could add these attributes to your <MaterialButton> tag:

android:insetTop="0dp" android:insetBottom="0dp" 
like image 38
Ben P. Avatar answered Sep 28 '22 05:09

Ben P.