Search code examples
onclickonclicklistenergestureontouchlistenerontouch

Difference between Click, Touch and Gesture in Android?


I am new to Android developing and there it is not very clear to me the difference between the Click, Touch and Gesture classes in Android. Is on the generalization of the other?


Solution

  • If you're talking about specific classes its always good to include the fully qualified name so as to avoid ambiguity.

    Click is pretty generic so I assume your talking about android.view.View.OnClickListener. This is an interface your widget class can implement to use the call back method onClick(). Any code inside the onClick() method is executed when you press that view (button).

    button.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {
                     // Perform action on click
                 }
             });
    

    Touch android.view.View.OnTouchListener The onTouchListener is an interface that exposes the onTouch() callback method and gives you access to the android.view.MotionEvent members like ACTION_BUTTON_RELEASE. The MotionEvent class is very powerful for movement related behaviour.

    Below example is from thread https://stackoverflow.com/a/11690679/1005142

    imageButton.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP){
    
            // Do what you want
            return true;
        }
        return false;
    }
    

    });

    Gesture android.view.GestureDetector.OnGestureListener This class is used to pick up how the user gestures their finger with your UI. There is already a lot of information on the Android dev site in the gesture section http://developer.android.com/training/gestures/detector.html. An example of using this class would be if you were writing fluidly with your finger on the keyboard where you need to listen for touch, movement and acceleration.