Skip to content Skip to sidebar Skip to footer

How Can I Get Button Pressed Time When I Holding Button On

I have a button, I press it and continue to holding it, if holding time exceeds certain time interval it fires some kind of intent, how can i do this. Thanks

Solution 1:

try this

You can use Touch Listener to do this.

Try:

Handlerhandler=newHandler();
    b.setOnTouchListener(newView.OnTouchListener() {

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

            default:
                handler.removeCallbacks(run);
                break;

            }
            returntrue;
        }
    });

Where b is the view (in your case it should button) on which you want to make long click.

And Runnablerun is as follows

Runnablerun=newRunnable() {

    @Overridepublicvoidrun() {
        // Your code to run on long click

    }
};

Hope it will help... :)

Solution 2:

Detect [ACTION_DOWN][1] and [ACTION_UP][2] events.

When button is pressed ([ACTION_DOWN][1]), start timer, measure time... If timer exceeds, invoke Intent. When button is released ([ACTION_UP][2]) stop timer.

finalTimertimer=newTimer();
button.setOnTouchListener(newOnTouchListener() {
    @OverridepublicbooleanonTouch(View arg0, MotionEvent arg1) {
        switch ( arg1.getAction() ) {
        case MotionEvent.ACTION_DOWN:
            //start timer
            timer.schedule(newTimerTask() {
                @Overridepublicvoidrun() {
                    // invoke intent
                }
            }, 5000); //time out 5sreturntrue;
        case MotionEvent.ACTION_UP:
            //stop timer
            timer.cancel();
            returntrue;
        }
        returnfalse;
    }
});

When button will be pressed, timer will be started. If button will be released before 5 seconds timeout, nothing will happen, otherwise your desired action will be executed.

Solution 3:

long lastDown;
long keyPressedDuration ;

button.setOnTouchListener(new OnTouchListener() {
     @Override
     public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
           lastDown = System.currentTimeMillis();
        } elseif (event.getAction() == MotionEvent.ACTION_UP) {
           keyPressedDuration = System.currentTimeMillis() - lastDown;
        }
     }
  };

Solution 4:

You can override onTouchEvent and calculate the offset between ACTION_DOWN and ACTION_UP;

Code below will run a runnable after x amount of time and also remove it if ACTION_UP is called before x time. Also included how you can get an exact time value...

@OverridepublicbooleanonTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        // record the start time
        mStartTime = ev.getEventTime();
        mHandler.postDelayed(mTaskToRun, MAXIMUM_LONG_PRESS_INTERVAL);
    } elseif (ev.getAction() == MotionEvent.ACTION_UP) {
        // record the end time
        mEndTime = ev.getEventTime();

        longkeyPressedDuration= mEndTime - mStartTime;

        mHandler.removeCallbacks(mTaskToRun);
    }

    returnsuper.onTouchEvent(ev);
}

Solution 5:

I got an Exception when I used a Timer! You can also use a Handler like below -

finalHandlerhandler=newHandler();
finalRunnablerun=newRunnable(){
        @Overridepublicvoidrun() {
            //Do your Stuff!     
        }
    };
    button.setOnTouchListener(newOnTouchListener() {
            @OverridepublicbooleanonTouch(final View view, MotionEvent motion) {
                switch (motion.getAction() ) {
                    case MotionEvent.ACTION_DOWN:
                        handler.postDelayed(run, 5000); //5 seconds delayreturntrue;

                    case MotionEvent.ACTION_UP:
                        handler.removeCallbacks(run);
                        returntrue;
                        }
                        returnfalse;
                    }});

Post a Comment for "How Can I Get Button Pressed Time When I Holding Button On"