Android detects when you hold the button

I need to be able to speak when the user holds the button down and when the user releases. This is different from onClickListener and onLongClickListener. How will I do something like this?

For example, I press a button that starts the chronometer. (pseudo code)

if ButtonIsBeingPressed { chronometer start(); //and keep going } else chronometer stop(); //or on release or something } 
+6
source share
1 answer

Take a look at the OnTouchListener , it has MotionEvents for Down (click) and Up (release):

 view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()) { case MotionEvent.ACTION_DOWN: // Start break; case MotionEvent.ACTION_UP: // End break; } return false; } }); 
+14
source

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


All Articles