Can't use android:background with button from the new material components
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 meansMaterialButton
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 fromButton
andAppCompatButton
. One of the main differences is thatAppCompatButton
has a4dp
inset on the left and right sides, whereasMaterialButton
does not.
This mentions only left/right inset, but the Attributes section of the readme shows that all four insets are supported:
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"
../>
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.
Related videos on Youtube
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, 2022Comments
-
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 colorbut 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 over 2 yearsYou can use ImageButton instead of Button from material components. android:background is working on ImageButton.
-
-
humazed over 5 yearsno, I need to worry about it, as it still showing at the button and the top.
-
Ben P. over 5 yearsHave you tried setting
android:insetTop="0dp"
(andinsetBottom
) on your<MaterialButton>
tag? -
SinaMN75 about 5 yearsso how we can add gradient in materialButton?
-
suv almost 5 yearsnot working for me, the buttons are still having a dark black background
-
eugeneek over 4 yearsPlease note that version 1.1.0 in alpha status for now mvnrepository.com/artifact/com.google.android.material/material
-
Gabriele Mariotti over 4 years@eugeneek Yes it is still in alpha, it will be in beta this week.
-
CoolMind almost 4 yearsThis is not a solution. See stackoverflow.com/a/52751122/2914140 or solutions below.
-
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 over 3 yearsyou safe my life. thanks for your solution.
-
Zeus Almighty almost 3 yearsDoesn't work, did you add something else to make it work? Just setting background and tint to null doesn't work
-
Siru malayil almost 3 years@ZeusAlmighty please tell me your reqirements, and how you tried
-
Zeus Almighty almost 3 yearsI tried it by setting
background=@drawable/custom_bg
andbackgroundTint="@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 almost 3 years@ZeusAlmighty can u share ur material button and style.xml file
-
Zeus Almighty almost 3 yearsHow do I send it?
-
Siru malayil almost 3 yearsif you didn't found any solution, please contact me@[email protected] or 8907959938 whatsApp