0

I want to customize a button using my custom drawable file as follows:

custom_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#48cae4" />
    <corners android:radius="10dp" />
</shape>

But after changing the background of the button(as follows) there is no effect

android:background="@drawable/custom_button" 

But if I change <style name="Theme.A" parent="Theme.MaterialComponents.DayNight.NoActionBar"> to <style name="Theme.A" parent="Theme.AppCompat.DayNight.NoActionBar">, then custom drawble takes effect on the button, but then the Material Components I used, convert to normal widgets.

Levi
  • 187
  • 2
  • 17

1 Answers1

1

You cannot use custom drawable for MaterialButton or using Material Theme. You can only set solid color states and set them with app:backgroundTint attribute. Ref

Create tint.xml inside res/color/ directory:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?attr/colorPrimary" android:state_pressed="true"/>
    <item android:color="@color/colorSecondary" />
    ...
</selector>

Use it in your button or button style:

app:backgroundTint="@color/tint"
Ananta Raha
  • 1,011
  • 5
  • 14