0

I'm trying to change the color of a button, but it doesn't work.

btn_sign_in.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="23dp" />
    <size android:height="16dp" />
    <solid android:color="#045762" />
</shape>

and I gave android:background="@drawable/btn_sign_in" in the button xml.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Mohmd labeeb
  • 156
  • 8

4 Answers4

3

You don't need to use a custom background.
Just use:

    <com.google.android.material.button.MaterialButton
        app:cornerRadius="23dp"
        app:backgroundTint="#045762"
        android:text="BUTTON"
        .../>

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

hi have you tried app:backgroundTint="#045762", this will work in may cases but if u have to use a drawable then do this

app:backgroundTint="@null"
android:background="@drawable/btn_sign_in"

When using MatertialComponents Background tint will be given priority over the background and in your case, you can try to pass color directly as backgroundTint

also check out MaterialButton from MaterialComponents

  • I also need to give elevation for the button, giving elevation to the button doesn't seem to work – Mohmd labeeb Dec 08 '20 at 09:28
  • u can give elevation to MaterialButton check out https://material.io/develop/android/components/buttons and by default, Material buttons have elevation and inSet already applied – trinadh thatakula Dec 10 '20 at 12:20
0

You can change the background color like this.

<Button
    android:background="@android:color/green"
    android:textColor="@android:color/white" />
Son Truong
  • 13,661
  • 5
  • 32
  • 58
0

XML:

<Button
android:background="@android:color/white"
android:textColor="@android:color/black"/>

Programmatically:

btn.setBackgroundColor(Color.WHITE);
btn.setTextColor(Color.BLACK);