29

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.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
humazed
  • 74,687
  • 32
  • 99
  • 138
  • You can use ImageButton instead of Button from material components. android:background is working on ImageButton. – bigant02 Aug 27 '21 at 09:11

9 Answers9

25

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"
    ... />
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Please note that version 1.1.0 in alpha status *for now* https://mvnrepository.com/artifact/com.google.android.material/material – eugeneek Oct 04 '19 at 06:14
  • 1
    @eugeneek Yes it is still in alpha, it will be in beta this week. – Gabriele Mariotti Oct 04 '19 at 06:37
  • @GabrieleMariotti, I often see you use alpha and beta versions of support libraries. How do you know when they obtain a new functionality? Do you work at Google? :) – CoolMind May 12 '20 at 12:50
19

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"
Ben P.
  • 52,661
  • 6
  • 95
  • 123
10

Looking at https://medium.com/@velmm/material-button-in-android-e4391a243b17 I found that app:backgroundTint (and app:backgroundTintMode) works. It changes a color, but not a drawable selector.

Also you can replace <Button with <android.widget.Button.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
10

If you want to use gradient drawable as MaterialButton's background,

set Your MaterialButton as below:

<com.google.android.material.button.MaterialButton
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_margin="16dp"
     app:backgroundTint="@null"
     android:background="@drawable/group_47"
     app:layout_constraintEnd_toEndOf="@+id/input_password"
     app:layout_constraintStart_toStartOf="@+id/input_password"
     app:layout_constraintTop_toBottomOf="@+id/input_password" />
ThaiPD
  • 3,503
  • 3
  • 30
  • 48
Siru malayil
  • 197
  • 2
  • 11
8

If you wish to keep your

android:background="@drawable/button_background"

And have MaterialButton respect it, then you must set

app:backgroundTint="@null"
app:backgroundTintMode="add" // it doesn't matter the value, but it must be set

Please note that you can also use app:background instead, although I've noticed enough breaking changes that I still prefer the method above.

RodXander
  • 643
  • 7
  • 12
2

I face the same issue when I use state drawable in a Button but It does not change the background of the button. After searching for a long time, I found 2 solutions as below:
The first solution is change the application theme from MaterialComponents to AppCompat in values/themes.xml file. then state drawable will work well.

to

    <style name="Theme.MyApplication" parent="Theme.AppCompat.DayNight.DarkActionBar">

If you still want to use MaterialComponents theme then you can try the second solution.
Use <android.widget.Button instead of <Button or <com.google.android.material.button.MaterialButton

    <android.widget.Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/state_test"
        android:text="Button" />

The second solution I found at Here

Trung Đoan
  • 643
  • 7
  • 18
1

The main reason for this decision by the google design team to exclude the functionality android:background="@drawable/" from the initial release versions of the material library is to enable developers to build consistent and professional-looking designs for apps at a faster pace. This is because most developers like me are bad in making decisions related to design and colors of the app.

Also, I found this snippet from google tutorial while migrating to MDC.

replace with this code

0

Just by using android:backgroundTint

<style name="MyButtonStyle" parent="Widget.MaterialComponents.Button">
    <item name="android:backgroundTint">@color/pink</item>
</style>


<com.google.android.material.button.MaterialButton
    android:id="@+id/followButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/channel_header_item_margin"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/titeChannelTV"
    style="@style/MyButtonStyle"/>
baderkhane
  • 307
  • 2
  • 10
0

using a simple

app:backgroundTint="@null"

with button attributes works perfectly.

Mudit Goel
  • 196
  • 1
  • 5