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:
Useful code, and thanks. One fix though - the detection of a swipe up/down should depend upon velocityY, not velocityX!
Thanks...for your useful code.
It's good Sample code!!
Thank you so much for this. I had the same issue I could not get a code snippet on gestures.
activity class has no other method than ontouchEvent to override
compiler is giving an error in my case :(
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??
cool ... this is really an excellent example! thanks for that. :)
Thack you Coll!!!!
hi do u have any idea for creating fingerprint scanning in android application
i also have the same question i want to trace my fingerprint over screen ,can any one tell me how can i do this
Very very helpful. It resolved all problems in gesture detection! ... Can you also provide another sample code for motion detection?
helped a lot. Thanks :)
Hi.. Thanks for the great sample. It solved my app purpose.. good work done.. Please keep posting several samples on gestures.. Sai :)
Thank you.....
Great code.....
Very useful for me......
by Jai.
Good one.. Exactly what i was looking for..
great sample code
exactly what I was looking for.
Thanks a lot,
Michael
hi! how could we apply this gesture to an image? thanks!
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!
Very nice code, helps me alot! Thanks!
Thanks a lot. It was very difficult to find some working example
Ty very much really helped a lot!
Thanks allot.u r the champ.
i want to thank you. Your code works fine
thank you :).. worked for me
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
Perfect~
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?
at first thanks and second this values are optimal
or can be change to be more better
Thank you. It is very helpful, and worked for me...
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.
No las ca-i fain.
This code can works with layouts xml?
how can put animation like contact call in Samsung phone on list view on swipe
Thanks in adv
please reply
Give your Number
Post a Comment