11

How can I detect clicked, pressed and released states of a Button. I want to perform different functions on these states. On click I want to call function1, on press I want to call function2 and on receive I want to call function3.

We can detect click state using View.OnClickListener. We can detect Pressed and Released states of a Button using View.OnTouchListener and handling ACTION_DOWN and ACTION_UP. I am able to detect these states individually, however, not together.

Below is code for OnCLickListener.

button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            System.out.println(" clicked ");
        }
    });

Below is 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 click and touch listeners on a Button, Click event never gets called. Instead I receive pressed and released state.

How can I handle these three states together?

EDIT:

I added the OnClickListener and OnTouchListener code I have used.

rm8x
  • 9,135
  • 1
  • 13
  • 18
Nitesh Kumar
  • 5,370
  • 5
  • 37
  • 54

4 Answers4

13

Easy since Button is a View:

    button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                // Pressed
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                // Released
            }
            return true;
        }
    });
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MineConsulting SRL
  • 2,340
  • 2
  • 17
  • 32
  • What about onclick event? I already explained in my question that I am able to handle press and release state separately but not with click. – Nitesh Kumar Dec 17 '14 at 11:58
  • What do you mean? For click there is another listener – MineConsulting SRL Dec 17 '14 at 12:02
  • I know for click there is OnClickListener. When I set both the listeners (OnClick and OnTouch) on the button then even if I click on the button OnCLick never gets called. Instead pressed-released gets called. – Nitesh Kumar Dec 18 '14 at 07:43
5

Change the return true; inside case MotionEvent.ACTION_DOWN: and case MotionEvent.ACTION_UP:to return false; or break;

Pranav Karnik
  • 3,318
  • 3
  • 35
  • 47
0

See this link. You can handle click manually in MotionEvent.ACTION_UP event by

button.performClick();
Community
  • 1
  • 1
Yousef khan
  • 2,764
  • 3
  • 14
  • 16
-1

clicked event include pressed and released state,if you want to fire clicked event,put method after ACTION_UP

ruby
  • 61
  • 1
  • 5