Can't change button's background color in XML .Android studio
Asked Answered
S

17

26

Android studio screenshot

I created TableLayout then buttons. The button's color was automatically set purple. So I can't change them right now. It's my first time here.

Soren answered 6/11, 2020 at 13:8 Comment(0)
C
45

don't use anything else use

<androidx.appcompat.widget.AppCompatButton
<!-- attributes-->
/>

instead of one

<Button
<!--attributes-->
/>

every thing will work nicely.

Happy coding.

Cobalt answered 7/12, 2020 at 9:38 Comment(1)
Worked for me. But can you please elaborate what is the issue with the "Button"? ThanksSplashy
I
29

It's purple because of default background Tint color. You can : change **app:backgroundTint ** instad of android:backgroundColor . In this case your backgroundTint will appear instead of background color

OR

add

app:backgroundTint="@null"

and after that your background color will appear.

OR

You can change the default theme in android manifest . For example :

android:theme="@style/Theme.AppCompat"

or

 android:theme="@style/Theme.AppCompat.NoActionBar"
Intelsat answered 27/11, 2020 at 15:31 Comment(2)
The first one worked for me. Thank you.Roof
This is what I was looking for, thank you!Ecliptic
K
20

Android default color is Purple in the latest Android Studio version. To change the color of the Button, you need to add a single line of code in the XML i.e.

app:backgroundTint="@color/red"

That's all!

Kerley answered 14/12, 2020 at 4:17 Comment(0)
B
6

Add the attribute:

app:backgroundTint="@null"

Bensen answered 16/4, 2021 at 15:35 Comment(0)
B
5

You might be using targetSdkVersion 30

Solution: change the theme.xml style from

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">

to

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Bag answered 19/2, 2021 at 0:7 Comment(0)
L
2

I had same problem, I tried almost everything and nothing worked! Seems like something has changed in Android Studio 4.1.1? idk.

[MY SOLUTION] So I used TextView instead of Button and set custom background and it worked for me :/ Hope this helps!

enter image description here

Livraison answered 12/11, 2020 at 18:22 Comment(0)
P
2

You can change style from design editor as shown in below image

enter image description here

Pneumectomy answered 17/5, 2021 at 10:58 Comment(0)
U
2

As some other guys mentioned, it might be that your theme overrides your button color

materialButtonStyle in particular

You can

  • either change your theme to Theme.AppCompat.DayNight.NoActionBar (or something like this)
  • or override materialButtonStyle parameter
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="MyButtonStyle" parent="TextAppearance.AppCompat.Button" />
    <style name="Theme.GradientTest" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

        <item name="materialButtonStyle">@style/MyButtonStyle</item>
        <!-- Other params here. -->
    </style>
Ulceration answered 20/12, 2022 at 11:16 Comment(0)
S
1

enter image description here

You can use this button

<androidx.appcompat.widget.AppCompatButton>
Satori answered 28/12, 2020 at 7:4 Comment(0)
A
1

Remove this from your button xml

android:background="@color/white"

And replace it with

android:backgroundTint="@color/white"
Adlay answered 20/3, 2022 at 14:4 Comment(0)
A
0

It's really easy. You should create a shape in XML. Set background to that shape then set background with that XML.

Aniseed answered 6/11, 2020 at 13:11 Comment(0)
P
0

it worked well with the update on 'com.google.android.material:material:1.2.0-alpha06'

or

do it programmatically : myButton.background = ContextCompat.getDrawable(requireContext(), R.drawable.my_background)

both options, you'll need to set app:backgroundTint property as null

app:backgroundTint="@null"

check the next link: https://github.com/material-components/material-components-android/issues/889

Pompei answered 25/11, 2020 at 0:26 Comment(1)
app:backgroundTint="@null" did the trick. ThanksLueck
G
0

go to res-values-themes-themes you will find two themes. one is when dark mode is activated. Go and change the one in dark mode. what could be happening is you have a different style for dark mode. I really hope it was easy to understand since english is not my first language

Glob answered 6/12, 2020 at 5:59 Comment(0)
D
0

To resolve the issue. In the xml replace background in "android:background="@android:color/black" with backgroundTint "android:backgroundTint="@android:color/black"

Dobbin answered 6/1, 2021 at 10:45 Comment(0)
S
0

I found a solution. inside the button tag, use

android:backgroundTint="#ccc"

replace #ccc in the above line with hex code for your preferred color. This should solve it.

Stygian answered 22/7, 2021 at 13:51 Comment(0)
U
0

use hexcode directly to change the background

android:backgroundTint="#A9A9A9"

Unhandled answered 12/4, 2022 at 10:52 Comment(0)
I
0

Use the function setBackgroundColor() on your button in the button click listener

Insomnia answered 16/6, 2022 at 5:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.