0

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Mariusz
  • 1,825
  • 5
  • 22
  • 36

3 Answers3

1

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

   private boolean isPressed = false;


    mYourButton.setOnClickListener(new OnClickListener(){

           @Override
            public void onClick(){

                 if(isPressed==false){

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

                 }else if(isPressed==true){

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

                  }
               }
          });
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
1

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"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/finalizar_active" android:state_pressed="true"/>
    <item android:drawable="@drawable/finalizar"/>
</selector>

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

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bg_purple_active" android:state_pressed="true"/>
    <item android: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 https://i.stack.imgur.com/UZMtt.png

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

0

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.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187