Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android. How do I keep a button displayed as PRESSED until the action created by that button is finished?

I have button_focused, button_pressed, and button_normal images. When I press the button, the button_pressed image is displayed and the action related to the button pressing begins.

When I quit pressing the button, the action continues but the button returns to button_normal image being displayed.

How can I set the button image being displayed to button_pressed during the entire action then reset to the button_normal image?

Thank you for your time

like image 893
user527405 Avatar asked Jan 12 '11 22:01

user527405


People also ask

What happens to activity when home button is pressed?

When Home button is pressed, onStop method is called in your activity. So what you may do is to add finish(); in onStop method to destroy your activity. Eventually onDestroy method will be raised to confirm that your activity is finished.

What is onClick method in Android?

When the user clicks a button, the Button object receives an on-click event. To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.

Which method is called when a button is pressed?

This answer is useful. 0. This answer is not useful. Show activity on this post. The activities onPause() is called when pressing the home or back button.

What happens when home button is pressed in Android?

Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background.


3 Answers

I used a function like

void setHighlighted(boolean highlight) {
    button.setBackgroundResource( highlight
                                ? R.drawable.bbg_pressed
                                : R.drawable.button_background);
}

where button_background is a selector defined in button_backgroung.xml:

<?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/bbg_pressed"></item>
    <item android:state_focused="true" android:drawable="@drawable/bbg_selected"></item>
    <item android:drawable="@drawable/bbg_normal"></item>
</selector>

That is, this code does not interfere with the pressed state used by the Android framework; instead, it changes the background so that the button looks pressed.

like image 72
18446744073709551615 Avatar answered Nov 14 '22 23:11

18446744073709551615


To make it a bit clearer if you are just in need of a two state button:

You do not need your own button.xml. You can work with the normal of Android.

The button.setPressed(true) will not work if you are clicking the button, because Android will reset it once you let go of the button. Try to set another buttons setPressed state first to see the effect.

Which means, to use it on the same button it must be set delayed. Here is a working example. Of course, the mentioned approach (by long id 18..) on changing the background works too.

   private final Handler mHandler = new Handler();
   rootView.findViewById(R.id.yourButton).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean pressed = false;
            if (v.getTag() instanceof Boolean)
                pressed = (boolean) v.getTag();
            final boolean newPressed = !pressed;
            // setTag to store state
            v.setTag(newPressed);
            final View vRun = v;
            Runnable run = new Runnable() {
                @Override
                public void run() {
                    vRun.setPressed(newPressed);
                }
            };
            mHandler.post(run);
            // mHandler.postDelayed(run, 5);
        }
    });
like image 26
Gunnar Bernstein Avatar answered Nov 14 '22 22:11

Gunnar Bernstein


If you change the image in the button manually in its onClick method, then when an action finishes it can set the normal image for that button back. If the action is very quick then change will not show properly - it may need a delay code as well.

like image 34
Lumis Avatar answered Nov 14 '22 23:11

Lumis