0

I have implemented a few gesture and touch control methods, But for some reason the app is installed but it doesnt seem to work. i.e the text doesnt change from the default one. I am using an actual device connected to my pc( testing via usb debugging). Thanks for the help.

package com.example.kheriaa.gestures;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;

public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener {
    private TextView mainMessage;
    private GestureDetectorCompat myGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainMessage =  (TextView) findViewById(R.id.MainMessage);
        this.myGestureDetector = new GestureDetectorCompat(this,this);
        myGestureDetector.setOnDoubleTapListener(this);
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        mainMessage.setText("onSingleTapConfirmed");
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        mainMessage.setText("Double");
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return true;
    }
}
supersayan
  • 13
  • 5

1 Answers1

2

You missed to dispatch touch events to your GestureDetector. Add that function to your view:

@Override
public boolean onTouchEvent(MotionEvent event){
    this.myGestureDetector.onTouchEvent(event);
    // Be sure to call the superclass implementation
    return super.onTouchEvent(event);
}

Refering to the documentation:

GestureDetector.OnGestureListener notifies users when a particular touch event has occurred. To make it possible for your GestureDetector object to receive events, you override the View or Activity's onTouchEvent() method, and pass along all observed events to the detector instance.

Now, it should works fine.

EDIT: Add this function to your View, not to your Activity directly!

N0un
  • 868
  • 8
  • 31