Reputation: 33
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>
I think it might be work fine, but it still purple color :(
Upvotes: 3
Views: 3842
Reputation: 414
if it's not much important, change Button to TextView and set background, it's working as magicially ;))
Upvotes: 0
Reputation: 144
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'
Upvotes: 0
Reputation: 912
if you are using Android Studio 4.1.1 you are probably using Theme.MaterialComponents check your themes.xml file
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:
I hope this is useful.
Upvotes: 8
Reputation: 753
Use this in your java code.
button1.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#ff0000")));
Upvotes: 1
Reputation: 65
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
Upvotes: 0