amardeep
amardeep

Reputation: 492

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

Upvotes: 5

Views: 3873

Answers (1)

AnujMathur_07
AnujMathur_07

Reputation: 2596

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

Upvotes: 7

Related Questions