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)
I think this is because we still set
backgroundTintin our defaultMaterialButtonstyle: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 outbackgroundTint(eitherapp:backgroundTint="@null"orapp: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:backgroundTintinstead ofandroid: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
I still need set
app:backgroundTint="@null", else drawable background will not workingsolved with
app:backgroundTintChange 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.DarkActionBartoTheme.MaterialComponents.DayNight.DarkActionBar.BridgeBoth 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-alpha06Stay tuned, a new alpha release is coming this week.
Worked like magic, thank you for telling +1
Still not working for me.
background_button.xml:
background_button_text_color.xml:
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.
It doesn’t work for drawables, you can only use to change colors and it clears says “tint” 😃
Theme.MaterialComponents.DayNight.DarkActionBar.Bridge This one worked for me Thanks!!
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).
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-alpha06and still can not override theandroid:backgroundproperty with my own xml drawable. I’m usingTheme.MaterialComponents.DayNightFriday release! 🤩🙈