What EVENT LISTENER should be used for my Android application

In my application, when one image button is pressed and held, I should be able to calculate the time the image button is pressed. Can anyone help me out by providing some simple instructions or sample code. I am really stuck here. Is there any specific event listener for this particular requirement. I am writing this application specifically for touch screen phones.

+3
source share
2 answers

What do you want to use:

OnTouchListener touchListener = new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        Log.d(LOG_TAG, "Action is: " + action);

        switch (action){
        case MotionEvent.ACTION_DOWN:
            timeAtDown = SystemClock.elapsedRealtime();
            break;

        case MotionEvent.ACTION_UP:
            long durationOfTouch = SystemClock.elapsedRealtime() - timeAtDown;
            Log.d(LOG_TAG, "Touch event lasted " + durationOfTouch + " milliseconds.");
            break;
        }
        return false;
    }

};

button.setOnTouchListener(touchListener);

timeAtDown - long, , touchListener. , ; , . . 'click' , DOWN ( MOVE) UP.

+2
+1

Source: https://habr.com/ru/post/1747836/


All Articles