Search

Thursday, August 20, 2009

Android gestures detection sample code

I recently wanted to implement all the gestures and motion detection in my project such as onFling (swipeup, swipe down), onLongPress, singletap, showpress context menu etc in android and after searching for a long while did not find any sample program that has all the methods as example mainly for onFling and so i made a sample program with all these see the below code

package testt.manju.com;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class gridd extends Activity implements OnGestureListener
{ private LinearLayout main; private TextView viewA;

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;


private GestureDetector gestureScanner;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gestureScanner = new GestureDetector(this);
main = new LinearLayout(this);
main.setBackgroundColor(Color.GRAY);
main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
viewA = new TextView(this);
viewA.setBackgroundColor(Color.YELLOW);
viewA.setTextColor(Color.BLACK);
viewA.setTextSize(16);
viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
main.addView(viewA);
setContentView(main);
}
@Override
public boolean onTouchEvent(MotionEvent me)
{
return gestureScanner.onTouchEvent(me);
}
@Override
public boolean onDown(MotionEvent e)
{
viewA.setText("-" + "DOWN" + "-");
return true;
}
@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) {
// nothing
}

return true;
}
@Override
public void onLongPress(MotionEvent e)
{
Toast mToast = Toast.makeText(getApplicationContext(), "Long Press", Toast.LENGTH_SHORT);
mToast.show();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
viewA.setText("-" + "SCROLL" + "-");
return true;
}
@Override
public void onShowPress(MotionEvent e)
{
viewA.setText("-" + "SHOW PRESS" + "-");
} @Override
public boolean onSingleTapUp(MotionEvent e) {
Toast mToast = Toast.makeText(getApplicationContext(), "Single Tap", Toast.LENGTH_SHORT);
mToast.show();
return true;
}
}

you can change the threshold, swipe distance and max swip path for onFling Method as per your requirements if the code working mail me or you can suggest improvements

33 comments:

Ben Blaukopf said...

Useful code, and thanks. One fix though - the detection of a swipe up/down should depend upon velocityY, not velocityX!

Anonymous said...

Thanks...for your useful code.
It's good Sample code!!

Anonymous said...

Thank you so much for this. I had the same issue I could not get a code snippet on gestures.

Vikrant Singh said...

activity class has no other method than ontouchEvent to override
compiler is giving an error in my case :(

Uday Kiran said...

hey dude... Excellent example for gestures.. but when i tried this between the activities, the application is closed.. could u please correct me where im doing wrong??

Unknown said...

cool ... this is really an excellent example! thanks for that. :)

SkaWorld said...

Thack you Coll!!!!

Anonymous said...

hi do u have any idea for creating fingerprint scanning in android application

Anonymous said...

i also have the same question i want to trace my fingerprint over screen ,can any one tell me how can i do this

danial said...

Very very helpful. It resolved all problems in gesture detection! ... Can you also provide another sample code for motion detection?

Anonymous said...

helped a lot. Thanks :)

Anonymous said...

Hi.. Thanks for the great sample. It solved my app purpose.. good work done.. Please keep posting several samples on gestures.. Sai :)

jayachandran said...

Thank you.....
Great code.....
Very useful for me......
by Jai.

Venkateshwara Rao said...

Good one.. Exactly what i was looking for..

Anonymous said...

great sample code
exactly what I was looking for.

Thanks a lot,
Michael

Anonymous said...

hi! how could we apply this gesture to an image? thanks!

Enrique said...

Thanks a lot! Just a copy&paste bug.. you are looking for Math.abs(velocityX) instead of Math.abs(velocityY) in swipe up or down.

Cheers man!

Anonymous said...

Very nice code, helps me alot! Thanks!

Michal said...

Thanks a lot. It was very difficult to find some working example

Stelios said...

Ty very much really helped a lot!

Irfan said...

Thanks allot.u r the champ.

sevemedimsenipardus said...

i want to thank you. Your code works fine

Anonymous said...

thank you :).. worked for me

Anonymous said...

can u help me out in implementing talker along with views in same activity...i am not able to implement both at same time...plz help

Anonymous said...

Perfect~

Anonymous said...

If i implement this, the detection works on unused part of the activity, but if i have a table with textviews, there does not work...Any idea why?

Anonymous said...

at first thanks and second this values are optimal
or can be change to be more better

Anonymous said...

Thank you. It is very helpful, and worked for me...

Dattatraya said...

Thank you it is really helpful to me. I want some more. like i move a stick over scree and draw the shape according to movement.

Anonymous said...

No las ca-i fain.

alegg said...

This code can works with layouts xml?

Unknown said...

how can put animation like contact call in Samsung phone on list view on swipe

Thanks in adv
please reply

Unknown said...

Give your Number

Post a Comment

Other Interesting Articles



Related Article Widget by Yogith

Search Powered by Google