I am starting with android development. I would like to do a calendar activity. I have an activity with it implements OnGestureListener
, with methods like onFling()
, in order to detect swipe gestures
, and change the month of the calendar. This works fine.
After that, my calendar has several cells, one per day. I added to each cell a onclick
listener, in order to add a event for this day. When I do this, my gestureListener
stops working, and only click events on cell are captured, but swipe gestures dosen't work (onFling()
method is never called). What can I do?
This is my code:
public class Calendar extends Activity implements OnGestureListener{ ...
//OnFling method that detects the swipe gesture
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
}
else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getApplicationContext(), "Swipe up", Toast.LENGTH_SHORT).show();
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getApplicationContext(), "Swipe down", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(this, "exception", Toast.LENGTH_SHORT).show();
}
return true;
}
//This is the method attached to each cell for onclick event
public void showDialog(View v){
TextView dia = (TextView)v.findViewWithTag("dia");
Intent i = new Intent(this, DialogoCalendario.class);
i.putExtra("dia",(String) dia.getText() );
i.putExtra("mes", ((TextView) findViewById(R.id.tbMes)).getText().toString());
startActivity(i);
}