3
  • Android Stidio 4.1.1
    • SDK 29 (Android 10)

I trying to change button background color on xml, but it doesn't changed.

here's my code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"/>

</LinearLayout>

enter image description here

I think it might be work fine, but it still purple color :(

Zoe
  • 27,060
  • 21
  • 118
  • 148
LEE
  • 33
  • 1
  • 8

5 Answers5

8

if you are using Android Studio 4.1.1 you are probably using Theme.MaterialComponents check your themes.xml file

enter image description here

so you have to use this attribute

android:backgroundTint="#ff0000"

read this documentation for more information:

https://material.io/components/buttons

If you insist on using android:background you can change your button xml code like this to force it using appcompat :

<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ff0000"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

or you can change the theme of all your project by changing the theme in themes.xml like this:

enter image description here

I hope this is useful.

Ma Jeed
  • 832
  • 1
  • 4
  • 12
  • it work, but i really doest need backgroundTint option. there's any way to use only background ? – LEE Dec 28 '20 at 12:19
  • if you are using android studio 4.1.1 you probably using Theme.MaterialComponents "check the themes.xml in your project" if you use background attribute it will use the default color. read this documentation https://material.io/components/buttons – Ma Jeed Dec 28 '20 at 12:28
  • thx for the great answer, I think It would be better to use a different kind of theme. or downgrade Android Studio version :( – LEE Dec 28 '20 at 13:27
1

Use this in your java code.

button1.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#ff0000")));
Mrudul Tora
  • 715
  • 8
  • 14
0

May be Android Studio is showing you the cached view for xml. Try changing Emulator from above where it is selected Custom as per your image. If it doesn't update the colour, try Invalidate Cache and Restart you Android Studio

Monim Kaiser
  • 65
  • 1
  • 2
  • 7
0

Ensure all your app dependencies in your build.gradle(app) file are latest.

The latest as of 4th Feb 2021 in android studio 4.1.1 are the following

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
Khushboo Gandhi
  • 144
  • 2
  • 8
0

if it's not much important, change Button to TextView and set background, it's working as magicially ;))