Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of the color button when pressed in Android?

I have some buttons which I set its background color as red, green and blue separately. When I press the button, click event is generated, but there is no change in gui for the user to know the button is pressed. Android button’s default background grayish color changes to orange and come back to grayish color after releasing the pressed state. How to implement this on colored button?

like image 496
indira Avatar asked Jan 02 '13 05:01

indira


People also ask

How can I change button color when pressed?

To change a button's color every time it's clicked:Add a click event listener to the button. Each time the button is clicked, set its style. backgroundColor property to a new value. Use an index variable to track the current and next colors.

How can I change the button color in Android?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.

How do I change the color of my text buttons?

Use a semi-colon to separate the different style elements in the HTML button tag. Type color: in the quotation marks after "style=". This element is used to change the text color in the button. You can place style elements in any order in the quotation markers after "style=".


2 Answers

That is implemented via a StateListDrawable, represented by selector in XML. Reference: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

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

<?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>
like image 157
K-ballo Avatar answered Oct 23 '22 04:10

K-ballo


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

We need to create a buttonselector.xml file in the drawable folder

<?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 the state of the button

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 & #FFFFFF which represent background color of the button according to the state.

In you main.xml 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 you activity class add the following two lines

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

Hope it helps

like image 38
Vikas Avatar answered Oct 23 '22 06:10

Vikas