[Button] android:background not working
See original GitHub issueDescription: 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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:17
- Comments:63 (9 by maintainers)
Top GitHub Comments
I think this is because we still set
backgroundTint
in our defaultMaterialButton
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 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:backgroundTint
instead 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