Skip to content Skip to sidebar Skip to footer

Android:state_selected Not Working

I just want to change the image on button click using the selector in the XML file. android:state_pressed='true' is working while android:state_selected='true' not working. Here is

Solution 1:

Change your buttonselector to this.

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_pressed="true"
   android:drawable="@drawable/discount1" /> <!-- pressed state -->

  <item android:drawable="@drawable/login" /> <!-- default -->
</selector>

EDIT

To change image of button permanently, use the following code.

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/your_image1"/>

Then in java file, add this:

Button btn = (Button) findViewById(R.id.button1);

        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
               btn.setBackgroundResource(R.drawable.your_image2);
            }
        });

Post a Comment for "Android:state_selected Not Working"