Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set background color of button using Material Components library

My code:

<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 and using Theme.MaterialComponents.DayNight.NoActionBar.

like image 367
nicoqueijo Avatar asked Dec 02 '22 09:12

nicoqueijo


1 Answers

Ran into the same issue, setting app:backgroundTint="@null" was what worked for me. See full solution here: https://github.com/material-components/material-components-android/issues/889#issuecomment-621194246

like image 115
Robin Avatar answered Jan 21 '23 14:01

Robin