Skip to content Skip to sidebar Skip to footer

How To Keep 'pressed' Look Of A Button After Pressing It?

a regular button changes its look, when it's pressed. How can i keep this 'pressed' look on the button even after it is released?

Solution 1:

Possible solution if You don´t want to use ToggleButton, is to set boolean values in onClickListener

privateboolean isPressed = false;


    mYourButton.setOnClickListener(newOnClickListener(){

           @OverridepublicvoidonClick(){

                 if(isPressed==false){

                    mYourButton.setBackgroundResource(R.drawable.your_pressed_image);
                    isPressed=true;

                 }elseif(isPressed==true){

                      mYourButton.setBackgroundResource(R.drawable.your_default_image);
                      isPressed=false;

                  }
               }
          });

Solution 2:

There is someways to do it, i suggest by drawable and layouts files.

For example, you have a view where you have a "SEND" or "FINISH BUTTON", so that view in the folder layout is something like this:

<ImageButton
android:id="@+id/btnIdNext"android:contentDescription="@string/someDescriptionOfImage"android:layout_width="wrap_content"android:layout_marginTop="10dp"android:layout_height="wrap_content"android:src="@drawable/buttons_src"android:background="@drawable/buttons"android:onClick="someaction" />

As you can see you got two importants drawables, the src and the background. So, lets create that files

In the folder drawable we create the buttons_src.xml file

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@drawable/finalizar_active"android:state_pressed="true"/><itemandroid:drawable="@drawable/finalizar"/></selector>

In the folder drawable we create the buttons.xml file too

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@drawable/bg_purple_active"android:state_pressed="true"/><itemandroid:drawable="@drawable/bg_purple"/></selector>

what we got is four images, two for the unpressed state and two for the pressed state.

The previews next:

*Unpressed Button http://i.stack.imgur.com/UZMtt.png

*Pressed Button http://i.stack.imgur.com/1E0u4.png

Solution 3:

You can use a ToggleButton instead of a regular one, which saves it states after it gets pressed.

just assign it a pressed and unpressed textures using a selector, and it will save it pressed texture after you press it.

Post a Comment for "How To Keep 'pressed' Look Of A Button After Pressing It?"