material-components-android: [Button] android:background not working

Description: after I change my app them to Theme.MaterialComponents.NoActionBar , In my xml file Button label’s android:background="@drawable/login_btn_bg" not working,and I can’t set others color、shape or selector,they are not work.

Expected behavior: Button label’s android:background= can normal work , when I set my app them to Theme.MaterialComponents.NoActionBar or others Material thems

Source code: <style name="CommonAppTheme" parent="Theme.MaterialComponents.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/common_colorPrimary</item> <item name="colorPrimaryDark">@color/common_colorPrimaryDark</item> <item name="colorAccent">@color/common_colorAccent</item> <item name="buttonStyle">@style/commonButtonStyle</item> <item name="materialButtonStyle">@style/commonButtonStyle</item> </style>

<?xml version="1.0" encoding="utf-8"?> <resources> <style name="commonButtonStyle" parent="Widget.MaterialComponents.Button"> <item name="android:insetTop">0dp</item> <item name="android:insetBottom">0dp</item> <item name="android:insetLeft">0dp</item> <item name="android:insetRight">0dp</item> <item name="android:minHeight">0dp</item> <item name="android:textColor">@color/common_text_color</item> </style> </resources> <Button android:id="@+id/btnLogin" android:layout_width="match_parent" android:layout_height="38dp" android:layout_marginLeft="31dp" android:layout_marginTop="61dp" android:layout_marginRight="31dp" android:background="@drawable/btn_bg" android:gravity="center" android:text="@string/common_immediately" android:textSize="15sp" /> Android API version: Android 8.1

Material Library version: 1.0.0

Device: le pro 3

To help us triage faster, please check to make sure you are using the latest version of the library.

We also happily accept pull requests.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 17
  • Comments: 63 (9 by maintainers)

Most upvoted comments

I think this is because we still set backgroundTint in our default MaterialButton style:

https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/button/res/values/styles.xml#L45

So if you are using a custom android:background, you have to make sure to null out backgroundTint (either app:backgroundTint="@null" or app:backgroundTint="@empty"), so that your custom background doesn’t get tinted.

Or, if all you care about is the color of the button, then you can just use app:backgroundTint instead of android:background, but be sure to set it to a color selector like ours to handle disabled state:

https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/button/res/color/mtrl_btn_bg_color_selector.xml

I am using latest version com.google.android.material:material:1.2.0-alpha05. This problem still exists. I can’t override button color using background attribute in XML

It’s working in latest release 1.2.0

I still need set app:backgroundTint="@null" , else drawable background will not working

solved with app:backgroundTint

Change Theme in styles from Theme.MaterialComponents.DayNight.DarkActionBar to Theme.MaterialComponents.DayNight.DarkActionBar.Bridge It worked for me!

Hi, could you make a new alpha release with this fix? It’s quite important for me and others 😃 Thank you! @wcshi @hamed8080

To work around this issue, do one of the following:

  • Add app:backgroundTint="@null" in xml or programmatically - btn.setBackgroundTintMode(null);

  • Change Theme Theme.MaterialComponents.DayNight.DarkActionBar to Theme.MaterialComponents.DayNight.DarkActionBar.Bridge

Both the solutions works fine.

@wcshi It worked, thank you. I love how android SDK is becoming more about painful, hidden workarounds than a smooth development kit.

it worked well with the update on: com.google.android.material:material:1.2.0-alpha06

Stay tuned, a new alpha release is coming this week.

Still not working for me. <Button android:id="@+id/button_one" style="@style/ButtonStyle" android:text="@string/button_one" />

<style name="ButtonStyle"> <item name="android:textSize">32sp</item> <item name="android:textColor">@drawable/background_button_text_color</item> <item name="android:background">@drawable/background_button</item </style>

background_button.xml: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@android:color/white" android:state_pressed="true" /> <item android:drawable="@android:color/transparent" android:state_pressed="false" /> </selector>

background_button_text_color.xml: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@android:color/black" android:state_pressed="true" /> <item android:color="@android:color/white" android:state_pressed="false" /> </selector>

I am getting a black background instead of white when the button is pressed. Not sure what I’m doing wrong. Any help? I am on 1.2.0-alpha06, btw. Update: was able to get it working thanks to the help of a couple folks on stackoverflow.

Worked like magic, thank you for telling +1

Still not working for me.

<Button
     android:id="@+id/button_one"
     style="@style/ButtonStyle"
     android:text="@string/button_one" />
<style name="ButtonStyle">
     <item name="android:textSize">32sp</item>
     <item name="android:textColor">@drawable/background_button_text_color</item>
     <item name="android:background">@drawable/background_button</item
</style>

background_button.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" android:state_pressed="true" />
    <item android:drawable="@android:color/transparent" android:state_pressed="false" />
</selector>

background_button_text_color.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/black" android:state_pressed="true" />
    <item android:color="@android:color/white" android:state_pressed="false" />
</selector>

I am getting a black background instead of white when the button is pressed. Not sure what I’m doing wrong. Any help? I am on 1.2.0-alpha06, btw.

Update: was able to get it working thanks to the help of a couple folks on stackoverflow.

This problem continues, version 1.3.0-alpha03

Mentioned workaround by @wolasoft works well, but it is giving issues to other components. Please re-open so that it gets more stable.

fix it by change Theme.MaterialComponents.NoActionBar to Theme.MaterialComponents.NoActionBar.Bridge and it will work

Set background with xml drawable still not work. If someone wants a dirty workaround, just use TextView instead of Button.

solved with app:backgroundTint

Still not working for me , I set a drawable

It doesn’t work for drawables, you can only use to change colors and it clears says “tint” 😃

To work around this issue, do one of the following:

  • Add app:backgroundTint="@null" in xml or programmatically - btn.setBackgroundTintMode(null);
  • Change Theme Theme.MaterialComponents.DayNight.DarkActionBar to Theme.MaterialComponents.DayNight.DarkActionBar.Bridge

Both the solutions works fine.

Theme.MaterialComponents.DayNight.DarkActionBar.Bridge This one worked for me Thanks!!

Change Theme in styles from Theme.MaterialComponents.DayNight.DarkActionBar to Theme.MaterialComponents.DayNight.DarkActionBar.Bridge It worked for me!

this also works for me, didn’t need to add more xml / java code Thanks

just use for xml

app:backgroundTint=“@color/purple_700

that’s it and it will be change the button color

No need to change the Theme it should be “Theme.MaterialComponents.DayNight.NoActionBar” Or “Theme.MaterialComponents.DayNight.NoActionBar”

@ymarian I think they are related. I just updated to Material 1.2.1, and FAB was working like a charm, after the update it is not working properly

@smithaaron this bug is for MaterialButton not for FloatingActionButton

Yes, the provided solution with the Tint works. However, the issue with the drawable is still active. So the only option I guess is to change it programmatically if you want to perform color change on pressed button. (I’m talking about the normal Button from the AndroidX package, not the Material one).

I think this is because we still set backgroundTint in our default MaterialButton style:

https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/button/res/values/styles.xml#L45

So if you are using a custom android:background, you have to make sure to null out backgroundTint (either app:backgroundTint="@null" or app:backgroundTint="@empty"), so that your custom background doesn’t get tinted.

Or, if all you care about is the color of the button, then you can just use app:backgroundTint instead of android:background, but be sure to set it to a color selector like ours to handle disabled state:

https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/button/res/color/mtrl_btn_bg_color_selector.xml

This Worked like a charm too! Thanks

It didn’t work for drawables for me either, see my hack comment above


De: V Bhavna Haritsa notifications@github.com Enviado: lunes, 11 de mayo de 2020 9:24 Para: material-components/material-components-android material-components-android@noreply.github.com CC: Alejandro H. Cruz j.alejandro_1093@hotmail.com; Comment comment@noreply.github.com Asunto: Re: [material-components/material-components-android] [Button] android:background not working (#889)

solved with app:backgroundTint

Still not working for me , I set a drawable

It doesn’t work for drawables, you can only use to change colors and it clears says “tint” 😃

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/material-components/material-components-android/issues/889#issuecomment-626520916, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAOQVOVOJNCN5KW2RVTKV33RQ6R2TANCNFSM4KEUAY2A.

Using 1.2.0-alpha06 and still can not override the android:background property with my own xml drawable. I’m using Theme.MaterialComponents.DayNight

Friday release! 🤩🙈