1

I'm trying to modify some code I found online to my needs. It is supposed to catch a MotionEvent (a fling in particular) and launch a separate activity. I am new to java, so I'm having some trouble understanding the code. Here's what I have so far:

public class Hypertension extends Activity {
 private GestureDetector flingDetector;
 View.OnTouchListener gestureListener;
 private TextView redView;
 private TextView greenView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        redView = (TextView)findViewById(R.id.buttonRed);
        greenView = (TextView)findViewById(R.id.buttonGreen);

        redView.setOnTouchListener(gestureListener);
        greenView.setOnTouchListener(gestureListener);

        flingDetector = new GestureDetector(new MyFlingListener());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (flingDetector.onTouchEvent(event)) {
                    //startActivity(new Intent(Hypertension.this, Green.class));
                 return true;
                }
                return false;
            }
        };
    }

    class MyFlingListener extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
          float velocityY) {
         // TODO Auto-generated method stub
         startActivity(new Intent(Hypertension.this, Green.class));
         return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (flingDetector.onTouchEvent(event))
         return true;
     else
      return false;
    }
}

My understanding is that when the screen is touched, onTouchEvent is called. In the above code, this method is overridden to call flingDetector, which listens for a fling. If one is heard, it launches the activity.

As of now, however, nothing happens when I perform a fling in the emulator. Also, I am fairly confused as to what the return values of the various boolean methods actually represent.

dfetter88
  • 5,381
  • 13
  • 44
  • 55

2 Answers2

2

You have two onTouchEvent methods in your code. One is in the GestureDetector class (not overridden), and the other is in your Hypertension activity class (which you have overridden at the bottom).

When someone triggers the TouchEvent in the main activity you explicitly calling the GestureDetector one (passing down the event) here:

if (flingDetector.onTouchEvent(event)) return true;

But if you haven't overridden the onTouchEvent method of that class then nothing is going to happen!


The purpose of overriding these "onSomething()" methods is so that they will get called automatically when an event triggers. In general the way to work with listeners is as follows:

  1. Create a subclass of the Listener class for the event and override its "onEvent()" method to do something when the event is triggered
  2. Call the "setListener( Listener)" method of the object you want to trigger said events after it has been initialized--passing in your previously created Listener
  3. Sit back and watch :)
idolize
  • 6,455
  • 4
  • 24
  • 34
  • Thanks so much! I got it working. I simplified my app to respond to clicks instead of gestures and studied how it's syntax fit into your three step process. Once I understood that, it was just a matter of translating it into a swipe. Is there a way to mark this thread as solved? – dfetter88 Jul 02 '10 at 20:18
  • Great! Glad you got it working! And you already marked the thread as solved :) – idolize Jul 04 '10 at 18:33
0

For all event listeners that return a boolean it should return true when it handles the event, so in your example if the flingDetector handles the onTouchEvent it returns true.

This question has been posed before and there are some great answers there.

Community
  • 1
  • 1
Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • I read throught the page, and noticed I was missing the lines that defined the ontouchlistener for my textviews. I've updated the code above. However, the app is still not working. I think my main problem is that I don't understand how the methods are working together. What is my final method (onTouchEvent) doing exactly? If the textview is already set up to have a touch handler, it seems to me that this method is useless. – dfetter88 Jul 02 '10 at 17:55