Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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...

like image 643
amardeep Avatar asked May 06 '13 06:05

amardeep


1 Answers

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... :)

like image 191
AnujMathur_07 Avatar answered Sep 30 '22 20:09

AnujMathur_07