Fling Gesture in Android

By | April 12, 2012

On implementing the onGestureListener you can get the events like

Fling
LongPress
Scroll
ShowPress
SingleTap

Its really simple, otherwise we have to calculate the distance between the touchdown and touchup event to check a fling event is happened or not.


import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater.Filter;
import android.view.View.OnClickListener;
import android.widget.Toast;

public  class FlingActivity extends Activity implements OnGestureListener
{
	private GestureDetector gestureScanner;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		gestureScanner = new GestureDetector(this);
		setContentView(R.layout.main);
	}

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

	public boolean onDown(MotionEvent e)
	{
		Toast.makeText(FlingActivity.this, "Motion Event", Toast.LENGTH_LONG).show();
		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
	{
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

        Toast.makeText(FlingActivity.this, "fling Event ", Toast.LENGTH_LONG).show();
        return true;
	}

	public void onLongPress(MotionEvent e)
	{
		Toast.makeText(FlingActivity.this, "Long-Press event", Toast.LENGTH_LONG).show();
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
	{
		Toast.makeText(FlingActivity.this, "Scroll event", Toast.LENGTH_LONG).show();
		return false;
	}

	public void onShowPress(MotionEvent e)
	{
		Toast.makeText(FlingActivity.this, "Show-Press event", Toast.LENGTH_LONG).show();
	}

	public boolean onSingleTapUp(MotionEvent e)
	{
		Toast.makeText(FlingActivity.this, "Single-Tap event ", Toast.LENGTH_LONG).show();
		return true;
	}
}
Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Reply

Your email address will not be published. Required fields are marked *