humazed
humazed

Reputation: 76942

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

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.

Upvotes: 33

Views: 30187

Answers (9)

Mudit Goel
Mudit Goel

Reputation: 354

using a simple

app:backgroundTint="@null"

with button attributes works perfectly.

Upvotes: 3

Siru malayil
Siru malayil

Reputation: 355

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

Upvotes: 15

RodXander
RodXander

Reputation: 843

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.

Upvotes: 9

Dh. Yaduvanshi
Dh. Yaduvanshi

Reputation: 31

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

Upvotes: 1

baderkhane
baderkhane

Reputation: 307

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

Upvotes: 0

Trung Đoan
Trung Đoan

Reputation: 733

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

Upvotes: 2

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364684

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

Upvotes: 27

CoolMind
CoolMind

Reputation: 28845

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.

Upvotes: 10

Ben P.
Ben P.

Reputation: 54234

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"

Upvotes: 19

Related Questions