How to change the color of a color button when pressed on Android?

I have several buttons that I set the background color to red, green and blue separately. When I click the button, an event is fired, but there are no changes in gui, so that the user knows that the button is pressed. Android buttons by default: the grayish color changes to orange and returns to a grayish color after releasing the pressed state. How to implement this on a colored button?

+8
android android-button
source share
3 answers

This is implemented using StateListDrawable , represented by selector in XML. Link: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Below is an example of a drawable that will be white by default, black when clicked:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@android:color/black" /> <!-- pressed --> <item android:drawable="@android:color/white" /> <!-- default --> </selector> 
+26
source share

As already mentioned by K-Ballo, you can use StateListDrawable to implement a view with different graphics depending on the state. In your case, Button is a view in which two states are pressed on the button and the button is not pressed.

We need to create the buttonselector.xml file in the folder with the picture

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_not_pressed" android:state_pressed="false" android:state_selected="false"/> <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/> </selector> 

Create two separate xml files for button state

button_not_pressed.xml

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#FFFFFF" android:centerColor="#FFFFFF" android:endColor="#FFFFFF" android:angle="270" /> </shape> 

button_pressed.xml

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#FF0000" android:centerColor="#FF0000" android:endColor="#FF0000" android:angle="270" /> </shape> 

You will notice two html color codes # FF0000 and #FFFFFF, which represent the background color of the button according to the state.

In the main.xml file, where you set the style of your custom button

 <Button android:id="@+id/customButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/buttonselector" android:text="test" android:textColor="#000000" /> 

In your activity class add the following two lines

 Button customButton = (Button) findViewById(R.id.customButton); customButton.setBackground(getResources().getDrawable(R.drawable.buttonselector)); 

Hope this helps

+8
source share

Try this method:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" > <shape> <solid android:color="#1E669B" /> <stroke android:width="2dp" android:color="#1B5E91" /> <corners android:radius="6dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item> <shape> <gradient android:startColor="#1E669B" android:endColor="#1E669B" android:angle="270" /> <stroke android:width="4dp" android:color="#1B5E91" /> <corners android:radius="7dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> </selector> 
+2
source share

All Articles