Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Button] android:background not working #889

Closed
0xByteLeon opened this issue Jan 9, 2020 · 63 comments
Closed

[Button] android:background not working #889

0xByteLeon opened this issue Jan 9, 2020 · 63 comments
Labels

Comments

@0xByteLeon
Copy link

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.

@0xByteLeon 0xByteLeon added the bug label Jan 9, 2020
@pekingme
Copy link
Contributor

pekingme commented Jan 9, 2020

Hi @DreamerLeon, could you try with a newer version of the library? If possible, please provide a sample app to reproduce this issue. It can help us to understand the problem and make a decision to resolve it faster. Thanks.

@0xByteLeon
Copy link
Author

Hi @DreamerLeon, could you try with a newer version of the library? If possible, please provide a sample app to reproduce this issue. It can help us to understand the problem and make a decision to resolve it faster. Thanks.

New version(1.2.0-alpha03) also has this problem , I will write a demo Later on and post on github ,Thanks for your answer.

@0xByteLeon
Copy link
Author

Hi @DreamerLeon, could you try with a newer version of the library? If possible, please provide a sample app to reproduce this issue. It can help us to understand the problem and make a decision to resolve it faster. Thanks.

MDCIssueDemo
This project can reproduce this issue.

@pekingme
Copy link
Contributor

Thanks @DreamerLeon for the demo. This is a known issue. Currently the solution to this is to set the background programmatically. We are working on a solution for the xml attribute. Hope that it can land soon.

@0xByteLeon
Copy link
Author

Thanks @DreamerLeon for the demo. This is a known issue. Currently the solution to this is to set the background programmatically. We are working on a solution for the xml attribute. Hope that it can land soon.

Thx for your answer!

@hamed8080
Copy link

hope next beta fix this . I need add boilerplate code for background!

@ImAreeb
Copy link

ImAreeb commented 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

@diegoalvis
Copy link

Any update of this?
I changed my app theme due crash using Material TabLayout but this broke all the buttons and TextInputLayouts so I rolled back. Looking forward for next beta sadly can't use the TabLayout yet.

@0xByteLeon
Copy link
Author

@diegoalvis This problem still exits,I hope someone can fix it

@wcshi
Copy link
Contributor

wcshi commented Apr 1, 2020

Fixed in 309d718

@wcshi wcshi closed this as completed Apr 1, 2020
@AlejandroHCruz
Copy link

AlejandroHCruz commented Apr 8, 2020

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

@pejman-74
Copy link

Stills' waiting.🙄

@AlejandroHCruz
Copy link

@pejman-74 In case it's useful, my workaround is to use a FrameLayout with an ImageButton and a (Material)TextView

@wcshi
Copy link
Contributor

wcshi commented Apr 13, 2020

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

@KidusMT
Copy link

KidusMT commented Apr 17, 2020

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

@AlejandroHCruz
Copy link

Friday release! 🤩🙈

@NehaDShakya
Copy link

NehaDShakya commented Apr 20, 2020

Updated to : com.google.android.material:material:1.2.0-alpha06
Can onlyset button background color to @color/colorPrimary or #00000000.

@nicoqueijo
Copy link

nicoqueijo commented Apr 22, 2020

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.

@nickruddeni
Copy link

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

@nicoqueijo
Copy link

nicoqueijo commented Apr 22, 2020

@nickruddeni Check the update on the bottom of my post. Might help.

@AdislanSaidov
Copy link

the same problem. Using 1.2.0-alpha06 , still not working

@AdislanSaidov
Copy link

AdislanSaidov commented Apr 24, 2020

solved with app:backgroundTint

@dsn5ft
Copy link
Contributor

dsn5ft commented 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

@Bhavnaharitsa
Copy link

Still not working for me.

<style name="ButtonStyle"> 32sp @drawable/background_button_text_color @drawable/background_button

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.

Worked like magic, thank you for telling +1

@0xByteLeon
Copy link
Author

solved with app:backgroundTint

Still not working for me , I set a drawable

@Bhavnaharitsa
Copy link

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" :)

@AlejandroHCruz
Copy link

AlejandroHCruz commented May 11, 2020 via email

@dsn5ft
Copy link
Contributor

dsn5ft commented Jul 22, 2020

@cutiko If you want to use a custom drawable for CheckBox and disable the Material tinting, can you try setting app:useMaterialThemeColors="false"?

@mecoFarid
Copy link

Fixed in 309d718

@wcshi

Using:
Library version: 1.2.0
Android version: 29
Device: Pixel 3 emulator

Still not working

@wcshi
Copy link
Contributor

wcshi commented Aug 12, 2020

@mecoFarid please share a sample app and explain the expected result and observed result.

@mecoFarid
Copy link

@wcshi Here is the sample project. https://github.com/mecoFarid/MaterialCompButton

Observed result:
Button background color is reset to colorPrimary which is purple in the sample project.

Steps to get the expected result:

  1. In styles extend Theme.AppCompat.Light.DarkActionBar instead of Theme.MaterialComponents.Light.NoActionBar
  2. activity_main.xml use Button instead of com.google.android.material.button.MaterialButton
    You'll see a button with a red background which is the expected result.

@wcshi
Copy link
Contributor

wcshi commented Aug 12, 2020

Did you follow the steps in #889 (comment)?

@mecoFarid
Copy link

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

@varunam
Copy link

varunam commented Aug 27, 2020

It's working in latest release 1.2.0

@0xByteLeon
Copy link
Author

It's working in latest release 1.2.0

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

@whilson03
Copy link

still not working

Library version: 1.2.0

@smithaaron
Copy link

smithaaron commented Sep 8, 2020

This doesn't work for me on 1.2.1. This is what I've tried:

<com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_filter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/material_design_spacing_large"
            app:backgroundTint="@color/sort_fab_bg_selector"
            android:layout_gravity="end"
            app:maxImageSize="56dp"
            app:srcCompat="@drawable/ic_search_and_filter" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_filter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/material_design_spacing_large"
            app:backgroundTint="@null"
            android:background="@color/sort_fab_bg_selector"
            android:layout_gravity="end"
            app:maxImageSize="56dp"
            app:srcCompat="@drawable/ic_search_and_filter" />

Either way, the button is just black and the drawable isn't visible.

@ymarian
Copy link
Contributor

ymarian commented Sep 8, 2020

@smithaaron this bug is for MaterialButton not for FloatingActionButton

@CamiloVega
Copy link

@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

@Veisy
Copy link

Veisy commented Oct 5, 2020

This problem continues,
version 1.3.0-alpha03

@ABHISHEK-AMRUTE
Copy link

If you are concerned only about the android:background and not android:backgroundTint (as in my case), the following worked for me.
Set both android:background and android:backgroundTint programmatically
Here the android:backgroundTint is set to colour which I have used in my custom background.

        button1.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.create_lobby_button));
        button2.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.join_lobby_button));
        button1.setBackgroundTintList(this.getResources().getColorStateList(R.color.colorPrimaryDark));
        button2.setBackgroundTintList(this.getResources().getColorStateList(R.color.navigationbar_color));

@IamAashis
Copy link

solved with app:backgroundTint

Ya thanks it works

@hemantlakhotia204
Copy link

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

@HenryMacharia254
Copy link

I changed from Button to com.google.android.material.button.MaterialButton and all was well in that the theme colors were applied.

@BelalElhawary
Copy link

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

@KavinduDissanayake
Copy link

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"

@cholidridwan
Copy link

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

@yash10019coder
Copy link

still not works for me

this is code for my button
<Button android:id="@+id/button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginLeft="32dp" android:layout_marginEnd="32dp" android:layout_marginRight="32dp" android:gravity="center|left" android:background="@drawable/background_button" android:text="SEND" app:layout_constraintBottom_toTopOf="@+id/button3" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />
this is the background code for my button

`
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"

>
<solid android:color="#200540"

    />
<corners
    android:radius="100dp"
    />
<stroke android:color="@color/purple_500"
    android:width="10dp"
    />

`

@Swavis
Copy link

Swavis commented Jun 6, 2021

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.

@Fs0ci3ty19
Copy link

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!!

@ginagigo123
Copy link

Theme.MaterialComponents.DayNight.DarkActionBar
change to
Theme.AppCompat.DayNight.DarkActionBar works for me!

@riverorange78
Copy link

c# ms vs 2022
Theme.MaterialComponents.Light.DarkActionBar
change to
Theme.MaterialComponents.Light.DarkActionBar.Bridge works for me!

@Anshul-Jangra
Copy link

In my app I used gradient on button, it runs but sometimes button is shown in normal button color. Can anyone tell me what is the problem?

@Naught33
Copy link

Well the problem is that when I use drawables it looks okay on the xml design but once I install the app on my phone the color of the button goes back to default, althought the shape stays as I want it but the color ...

@WangberlinT
Copy link

still exist on 1.5.0

@Anshul-Jangra
Copy link

Thanks but it's resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests