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

21,673

Solution 1

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"

Solution 2

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"
    ... />

Solution 3

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.

Solution 4

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" />

Solution 5

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.

Share:
21,673

Related videos on Youtube

humazed
Author by

humazed

Mobile engineer with more than 6 years of Android development and 3 years of Flutter development. Experienced in working with international teams and clients. I like to work on challenging problems, especially ones that touch people's lives.

Updated on July 09, 2022

Comments

  • humazed
    humazed almost 2 years

    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.

    • bigant02
      bigant02 over 2 years
      You can use ImageButton instead of Button from material components. android:background is working on ImageButton.
  • humazed
    humazed over 5 years
    no, I need to worry about it, as it still showing at the button and the top.
  • Ben P.
    Ben P. over 5 years
    Have you tried setting android:insetTop="0dp" (and insetBottom) on your <MaterialButton> tag?
  • SinaMN75
    SinaMN75 about 5 years
    so how we can add gradient in materialButton?
  • suv
    suv almost 5 years
    not working for me, the buttons are still having a dark black background
  • eugeneek
    eugeneek over 4 years
    Please note that version 1.1.0 in alpha status for now mvnrepository.com/artifact/com.google.android.material/mater‌​ial
  • Gabriele Mariotti
    Gabriele Mariotti over 4 years
    @eugeneek Yes it is still in alpha, it will be in beta this week.
  • CoolMind
    CoolMind almost 4 years
    This is not a solution. See stackoverflow.com/a/52751122/2914140 or solutions below.
  • CoolMind
    CoolMind almost 4 years
    @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? :)
  • Trung Đoan
    Trung Đoan over 3 years
    you safe my life. thanks for your solution.
  • Zeus Almighty
    Zeus Almighty almost 3 years
    Doesn't work, did you add something else to make it work? Just setting background and tint to null doesn't work
  • Siru malayil
    Siru malayil almost 3 years
    @ZeusAlmighty please tell me your reqirements, and how you tried
  • Zeus Almighty
    Zeus Almighty almost 3 years
    I tried it by setting background=@drawable/custom_bg and backgroundTint="@null". I want to use the color of background drawable as background of Material button but it is using primary color as background.
  • Siru malayil
    Siru malayil almost 3 years
    @ZeusAlmighty can u share ur material button and style.xml file
  • Zeus Almighty
    Zeus Almighty almost 3 years
    How do I send it?
  • Siru malayil
    Siru malayil almost 3 years
    if you didn't found any solution, please contact me@[email protected] or 8907959938 whatsApp