Android Button Background Color

Android button background color

If you want to keep the general styling (rounded corners etc.) and just change the background color then I use the backgroundTint property

android:backgroundTint="@android:color/holo_green_light"

How to change the color of a button?

You can change the colour two ways; through XML or through coding. I would recommend XML since it's easier to follow for beginners.

XML:

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

You can also use hex values ex.

android:background="#FFFFFF"

Coding:

//btn represents your button object

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

Android Button background color not changing

I think you need to change your attribute from "theme" to "style". These files are intended for different things, so are used differently.

Style is used to assign attributes to individual views or components, and Theme is used to apply attributes to the entire app.

So try this instead:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnLogin"
android:id="@+id/btn_login"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
<!-- This attribute was changed -->
android:style="@style/ButtonStlye123"
android:onClick="login" />

You should also split these different types of XML into the correct files as appropriate (styles.xml and themes.xml - instead of keeping them all in the same file). This is convention, and won't effect the code compilation or execution, but is recommended as a code style.

Android Button background color not changing on xml

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

Sample Image

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:

Sample Image

I hope this is useful.

XML Button background color not changing

Try with AppCompatButtom instead of Button

Replace your Button With AppCompatButtom

 <androidx.appcompat.widget.AppCompatButton
android:id="@+id/next"
android:layout_width="305dp"
android:layout_height="64dp"
android:background="@color/colorTheme"
style="@style/ButtonStyle"
android:text="Next"
app:layout_constraintBottom_toTopOf="@+id/imageViewFooterGetStarted"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.968" />

Hope its work !!

Unable to change button background color on click

When I click the button the color changes and disappears I want it to stay until user press any other button

You have no code to do that. A Button does not have a durable state that changes when the use clicks on it. You will have to do something yourself, in Java/Kotlin code, to do that.

For example, you could toggle the activated state via setActivated() on the Button. Then, in your btn_selector resource, you could have different drawables for android:state_activated="true" than for android:state_activated="false".



Related Topics



Leave a reply



Submit