How to change the color of a button after clicking?

I create a button with a background color, but when I click on it, it shows nothing.
I need to show a different color on the button after the click, because the user needs to know the button - Click.
I do not understand how to do this?
Give me a 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> 
+5
source share
2 answers

// XML file saved in 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 XML layout will apply the color list to the 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> 
+9
source
 <?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> 

It will work

0
source

Source: https://habr.com/ru/post/1401178/


All Articles