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.