amardeep amardeep - 5 months ago 12
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...

Answer

You can try Touch Listener to do this.

Try:

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

        @Override
        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 */);
                break;

            case MotionEvent.ACTION_CANCEL:
                handler.removeCallbacks(run);
                break;

            case MotionEvent.ACTION_UP:
                handler.removeCallbacks(run);
                break;

            }
            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() {

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

    }
};

Hope it helps... :)