Capture event on Android after some seconds
Asked Answered
O

2

5

I want to add a button to my app in Android and capture the event on this button (onclick) after a couple of seconds pushing the button, so it doesn't react at the first touch. Is this possible to achieve on Android?

Right now I have the next code that captures an onclick on the home button (in the ActionBar).

public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        showSendLogAlert();
    }

    return super.onOptionsItemSelected(item);
}

When an user clicks on this button, it sends a little report by email, and I don't want to launch this event accidentally, that's why I want the user to push a couple of seconds in order to be sure he wants to do that operation.

Solution:

Based on the comments below I got this working solution:

@Override
protected void onCreate(final Bundle savedInstanceState) {
    // stuff

    // Set the home button clickable
    getActionBar().setHomeButtonEnabled(true);

    // Define a long click listener instead of normal one
    View homeButton = findViewById(android.R.id.home);
    homeButton.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            showSendLogAlert();
            return false;
        }
    });

    // more stuff
}
Octofoil answered 19/2, 2014 at 12:30 Comment(3)
are u asking about longclick() listenerMatrimonial
@Matrimonial I couldn't find the name of this specific event, thanks! that's exactly what I was meaning.Octofoil
just mention that there is longclikc listener is available not the exact listener nameMatrimonial
D
7

I've made something similar, but in my case I wanted to show a new activity if the button was pressed continuously for 3 seconds. Use this code for reference.

Runnable mRunnable,timeRunnable;
Handler mHandler=new Handler();

btnBackoffice = (Button) findViewById(R.id.btn_backoffice);

btnBackoffice.setOnTouchListener(buttonOnTouchListener);    

private OnTouchListener buttonOnTouchListener = new OnTouchListener() { 
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch ( event.getAction() ) {
        case MotionEvent.ACTION_DOWN:
            mHandler.postDelayed(timeRunnable, 3000);
            break;
        case MotionEvent.ACTION_UP:
             mHandler.removeCallbacks(timeRunnable);
            break;
        }
        return true;
    }
};

timeRunnable=new Runnable(){
        @Override
        public void run() {
            Intent intent = new Intent(MainActivity.this, BackofficeActivity.class);
            startActivity(intent);

        }
    };

Hope it helps.

Derrickderriey answered 19/2, 2014 at 12:39 Comment(1)
Thank you, that's what I was looking for. In my case I left the default time delay of the listener, but its good to know how to implement and specific delay.Octofoil
T
0

In your onClickListener for the button:

 myButton.setEnabled(false);

 Timer buttonTimer = new Timer();
 buttonTimer.schedule(new TimerTask() {

 @Override
public void run() {
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            myButton.setEnabled(true);
        }
    });
}
}, 5000));

This will disable the button when clicked, and enable it again after 5 seconds(5000 is a delay of 5 seconds).

If the click event is handled in class that extends View rather than in an Activity do the same thing but replace runOnUiThread with post.

Thinia answered 19/2, 2014 at 12:37 Comment(1)
Not bad implementation, but I think using the longClickListener is a cleaner approach. Thanks!Octofoil

© 2022 - 2024 — McMap. All rights reserved.