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
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.
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
Android:Change Button Text and Background Color
How Does One Implement Drag and Drop for Android Marker
How to Unpair Bluetooth Device Using Android 2.1 Sdk
Sqlite Query in Android to Count Rows
Clear All Activities in a Task
Display Toolbar for Google Maps Marker Automatically
Failed to Launch Emulator: Error: Emulator Didn't Connect Within 60 Seconds
Android - Periodic Background Service - Advice
Android Support Library Error After Updating to 23.3.0
Updated Sdk Version, Getting Classnotfoundexception: Android.Support.V4.View.Viewpager
How to Add a Hint in Spinner in Xml
How to Customize the Contextual Action Bar Using Appcompat in Material Design
Recyclerview Items with Big Empty Space After 23.2.0
Network_Provider Not Providing Updated Locations
Android Emulator Screen Fills Up Only Some Fraction Part of Actual Android Emulator Screen
How to Change Action Bar Actions Dynamically
Android: Custom Separator (Or Even Item) in Listview Depening on Content of Item