How to change the color of button after click?
17,335
Solution 1
//XML file saved at res/drawable/button_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
//This layout XML will apply the color list to a View:
<Button android:textSize="15px"
android:id="@+id/button9"
android:gravity="center|bottom"
android:textColor="@color/myWhiteColor"
android:drawableTop="@drawable/math"
android:text="@string/HomePage_Math"
android:background="@drawable/button_bg"
android:layout_width="54dp"
android:layout_height="wrap_content" ></Button>
Solution 2
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/colorPrimaryDark" />
<item android:state_focused="true" android:drawable="@android:color/holo_green_dark" />
<item android:drawable="@color/colorCartButton" />
</selector>
This will work
Author by
Sandip Armal Patil
Android Developer in Programr , Pune. My responsibility it to develop Android Application. I also work on HTML5, JavaScript. I love Reading. I like to travel and visit historical places. Here is my Android Blog. Here is my two Android App. Find Anything and We care baby
Updated on June 19, 2022Comments
-
Sandip Armal Patil almost 2 years
I create button with background color but when i click on it, it's not show anything.
I need to show different color on button after click because user need to know button is
Click.
I don't understand how to do this?
Give me suggestion.
here is my button code.<Button android:textSize="15px" android:id="@+id/button9" android:gravity="center|bottom" android:textColor="@color/myWhiteColor" android:drawableTop="@drawable/math" android:text="@string/HomePage_Math" android:background="@color/myMaroonColor" android:layout_width="54dp" android:layout_height="wrap_content" ></Button>
-
Sandip Armal Patil about 12 yearsIt show error "tts application has stop unexpectedly"...Not working
-
Padma Kumar about 12 years@sandiparmal save that xml in you drawable folder and try it as android:background="@drawable/button_bg".
-
Padma Kumar about 12 years@sandiparmal this error is not related to this code. its different error in you app.
-
Shaista Naaz over 11 years@SandipArmalPatil if you use drawable instead of color codes in the button_bg xml then you will not get this error "application has stop unexpectedly"
-
Rajat Anantharam over 10 years<item> tag requires a 'drawable' attribute or child tag defining a drawable. This piece of code will not work
-
Padma Kumar over 10 years@RajatAnantharam yes you need to report to google soon developer.android.com/guide/topics/resources/…
-
Rajat Anantharam over 10 yearsIts an easy fix. Define shape drawables with solid colors of choice for each states. I am in no hurry to report to Google :)
-
pa1pal about 10 yearsandroid:color is not a valid command in <selector> What I did to avoid crashing was create a color.xml file in values and define color in between <color name="gray">#808080</color> and give link to that in button_bg.xml .
-
Padma Kumar about 10 years@pa1pal might be you forgot to add inside it in <item>. see the 3rd comments from bottom.
-
Phiter almost 8 yearsWhy will it work? Add some extra details to make it a great answer.