How can I detect the pressed and pressed and released button states. I want to perform different functions in these states. On click, I want to call function1, I click, I want to call function2, and when I receive it, I want to call function3.
We can detect click state using View.OnClickListener . We can determine the state of a pressed and released button using View.OnTouchListener and processing ACTION_DOWN and ACTION_UP . However, I can detect these conditions individually, but not together.
Below is the code for OnCLickListener.
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println(" clicked "); } });
Below is the code for OnTouchListener.
button.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()) { case MotionEvent.ACTION_DOWN: System.out.println(" pressed "); return true; case MotionEvent.ACTION_UP: System.out.println(" released "); return true; } return false; } });
When I set the click and touch the listeners on the button, the Click event is never raised. Instead, I get pressed and freed state.
How can I handle these three states together?
EDIT:
I added the OnClickListener and OnTouchListener code that I used.
source share