0

I want to handle three different events for a button like when the button is TOUCHED, PRESSED and RELEASED. How is this possible? Please help me in this.

Here is my code that I have developed so far for handling two event but I need three events.

button.setOnTouchListener(new OnTouchListener() 
{
    public boolean onTouch(View v, MotionEvent event) 
    {

        // Check if the button is TOUCH
        if (event.getAction() == MotionEvent.ACTION_DOWN)  
        {

        }


        // Check if the button is RELEASED
        else if (event.getAction() == MotionEvent.ACTION_UP) 
        {   

        }


            return true;
        }
});
Android
  • 82
  • 1
  • 10
  • check this one http://developer.android.com/reference/android/view/MotionEvent.html use which suits your cause – Nihar Mar 26 '14 at 08:18
  • you need to add some time calculation event to differentiate b/w your `TOUCH` and `PRESSED` event. – maddy d Mar 26 '14 at 08:19

2 Answers2

0

What you have developed so far seems fine and should work for handling the touch and release events just fine. The long presses are a bit more complicated but nothing to fancy. You should preferrably use a GestureDetector. An example is given in this answer. Alternatively you can consult the official documentation.

Update: You may find the official tutorial on gestures quite useful. I believe especially the public void onLongPress(MotionEvent event) method will suit your needs!

Community
  • 1
  • 1
Eric Tobias
  • 3,225
  • 4
  • 32
  • 50
  • I have developed the code for handling two events but I need to handle three events i.e TOUCHED, PRESSED and RELEASED. How is this possible? – Android Mar 26 '14 at 08:42
  • The example documentation features code that will let you implement a simple `GestureListener` to deal with the events you want to handle. I am not sure I understand your comment, you can handle ANY event you wish. – Eric Tobias Mar 26 '14 at 08:53
-1

You will try this i hope it will work fine for your cases... You just use this Activity and use this method for your implementation all the best... You just use the below listener class for the implementation of long press only....

public class MainActivity extends Activity {

private Button button;  
int check=0;
private GestureDetector  detector;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button = (Button)findViewById(R.id.button1);
    detector = new GestureDetector(new GestureListener());
    check=0;
    button.setOnTouchListener(new OnTouchListener() {           
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            detector.onTouchEvent(event);
            int action = event.getActionMasked();               
            switch (action) {
            case MotionEvent.ACTION_MOVE:
                System.out.println("Move");
                break;
            case MotionEvent.ACTION_DOWN:
                System.out.println("Down");
                break;

            case MotionEvent.ACTION_UP:
                System.out.println("Released");
                break;              
            default:
                break;
            }
            return false;
        }
    });
}
class GestureListener implements OnGestureListener
{

    @Override
    public boolean onDown(MotionEvent e) {
        System.out.println("Down List");
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        System.out.println("Fly List");
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        System.out.println("Long press");

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY) {

        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

        System.out.println("Press List");

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        System.out.println("Single Tap List");
        return false;
    }

}
  }
Naveen Kumar Kuppan
  • 1,424
  • 1
  • 10
  • 12