question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Button] android:background not working

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

55reactions
dsn5ftcommented, Apr 29, 2020

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

53reactions
ImAreebcommented, Mar 8, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Background Drawable Not Working in Button Since ...
Just go to themes.xml under values folder in project window and change the MaterialComponents to AppCompact in both themes and button start ......
Read more >
Android Button Design, Custom Button, Round Button, Color
The following selector btn_bg_selector_disabled.xml is used on a button which is not enabled. ... For the above selector to work, we need to ......
Read more >
Why does Android button shape changes but not color ... - Quora
I guess we have two ways to solve this, either you change the material theme or change the button . Let's see one...
Read more >
How to Change the Background Color After Clicking the ...
When we click on the button the onClick function is called. To set the click handler event for the button we need to...
Read more >
A Poor Default (Or: Why Your Button Does Not Work)
For example, suppose you are a brand-new Android app developer. You install Android Studio 4.1 and use the new-project wizard. You add a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found