walnuty
walnuty

Reputation: 23

Background drawable not applying to Android button

I want to applying drawable to my android button. But, background drawable is not working.

This is my drawable code.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
    <shape android:shape="rectangle">
        <solid android:color="#FFEF1D1D" />
    </shape>
</item>
</layer-list>

and This is my layout code

<Button
            android:id="@+id/booking"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="6.5"
            android:text="bokking"
            android:textStyle="bold"
            android:textColor="@color/white"
            android:background="@drawable/booking_btn"
            android:layout_marginRight="10dp"/>

Drawable is not applied to Button, but drawable is applied to AppCompatButton. I wonder why too.

When I applied this code, Only backgroundTint applies to buttons.

I spent a week on this issue, but I couldn't solve it. plz help me

Upvotes: 1

Views: 413

Answers (4)

Nima Owji
Nima Owji

Reputation: 665

You didn't use the selector in your XML. Use Selector instead of Layer-List

Upvotes: 0

Sathyan
Sathyan

Reputation: 168

There is only one chance for that.I think you are using material theme.That's why the background property is not applying for button.If you want use that,change the theme to appcompat varaint.

Upvotes: 1

Abhay Koradiya
Abhay Koradiya

Reputation: 2117

You can't apply the background on the material button.

If you are using Theme.MaterialComponents.*, then the normal button also used as the material button forcefully.

You have two options.

  1. change it to any AppCompat theme.
  2. Still If you need a material theme for some components, then use any material theme with a bridge. like Theme.MaterialComponents.*.Bridge

So, If you will go with option 2, you can use material button as per your need.

use com.google.android.material.button.MaterialButton for material button else use your normal button.

Upvotes: 2

Sam Chen
Sam Chen

Reputation: 8917

Looks like there is a problem with the Theme since Android Studio 4.1, try changing the base Theme from Theme.MaterialComponents.DayNight.DarkActionBar to Theme.AppCompat.DayNight.DarkActionBar, it works for me.

Upvotes: 1

Related Questions