amardeep amardeep - 8 months ago 29
Android Question

How to Customize the time interval of long/delay button pressed in android

I am making an app's which have a button to performed an action, but i want to perform the action when user long press on the button.Since Google provides the long press time duration appx .5 sec but I want to customize this time duration. Please help...


You can try Touch Listener to do this.


Handler handler = new Handler();
    b.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View arg0, MotionEvent arg1) {
            switch (arg1.getAction()) {
            case MotionEvent.ACTION_DOWN:
                handler.postDelayed(run, 5000/* OR the amount of time you want */);

            case MotionEvent.ACTION_CANCEL:

            case MotionEvent.ACTION_UP:

            return true;

Where b is the view on which you want to make long click.

And Runnable run is as follows

Runnable run = new Runnable() {

    public void run() {
        // Your code to run on long click


Hope it helps... :)