user1634451
user1634451

Reputation: 5212

Android GestureListener not detecting events

I have looked at numerous examples online and they all have gesturelistener implemented this way. I can't get android to pick up the gesture event the value of myText doesnt' change and i don't get any output in logcat. What am i doing wrong?

public class MainActivity extends Activity {
private GestureDetector gDetector;
private TextView myText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    gDetector = new GestureDetector(this, new MyOnGestureListener());
    myText = (TextView) findViewById(R.id.mytext);
    myText.setText("Test");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);

    return true;
}

class MyOnGestureListener extends SimpleOnGestureListener implements
        OnGestureListener {
    @Override
    public boolean onDown(MotionEvent e) {
        Log.d("Main", "did");
        myText.setText("hi");
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        myText.setText("hi");
        Log.d("Main", "did");
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        Log.d("Main", "did");
        myText.setText("hi");
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY) {
        Log.d("Main", "did");
        myText.setText("hi");
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        Log.d("Main", "did");
        myText.setText("hi");
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        Log.d("Main", "did");
        myText.setText("hi");
        // TODO Auto-generated method stub
        return true;
    }
}

}

Upvotes: 5

Views: 2729

Answers (3)

premma
premma

Reputation: 373

You need to use your field gDetector. So far, you have only created an instance, but now you have to use it, to get the fruits out of it.

For example like this:

 this.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(final View view, final MotionEvent event) {
       return gDetector.onTouchEvent(event);
    }
 });

Upvotes: 4

znat
znat

Reputation: 13474

The missing part is that you don't link your GestureListener to a touch event.

So you can override OnTouchListener like this:

@Override
public boolean onTouchEvent(MotionEvent me) {
    return gDetector.onTouchEvent(me);
}

Or if you want to enable your GestureDetector on a specific view:

view.setOnTouchListener(new OnTouchListener{
    @Override
    public boolean onTouchEvent(MotionEvent me) {
       return gDetector.onTouchEvent(me);
    }
})

Upvotes: 7

Ali Imran
Ali Imran

Reputation: 9217

Try this code here

This should work.

also change the line

  gestureScanner = new GestureDetector(this);

to

  gestureScanner = new GestureDetector(ScrollTest.this, this);

Upvotes: 0

Related Questions