I am new to android programming. I want to handle the swipe event. Please help me to use appropriate class and methods with example.
-
1Your question is vague, try to clearly state what you want to do and what have you tried so far. You may want to look at Gesture detection OR ViewPager in android docs. – Sangharsh Nov 26 '14 at 07:13
-
Here is example http://androidexample.com/Swipe_screen_left__right__top_bottom/index.php?view=article_discription&aid=95&aaid=118 and http://www.learn-android-easily.com/2013/06/detect-swap-event-in-android.html – Jaydeep Nov 26 '14 at 07:15
-
Google is Your best friend my friend.. You will find enough examples .. if you have any problem with the code , then here comes stackOverflow ifor resque .. – Makwana Nov 26 '14 at 07:17
2 Answers
Android supports gestures. To use this support your application must use the view "GestureOverlayView". In this view you place your other views.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello"/>
</LinearLayout>
import java.util.ArrayList;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class GestureTest extends Activity implements OnGesturePerformedListener {
private GestureLibrary gestureLib;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
View inflate = getLayoutInflater().inflate(R.layout.main, null);
gestureOverlayView.addView(inflate);
gestureOverlayView.addOnGesturePerformedListener(this);
gestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!gestureLib.load()) {
finish();
}
setContentView(gestureOverlayView);
}
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
for (Prediction prediction : predictions) {
if (prediction.score > 1.0) {
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT)
.show();
}
}
}
}

- 209
- 1
- 6
As you are new to Android Programming, I suggest you to go through the Swipe Developer Standards once.
After that you might want to check out this solution : Android Basic Gesture Detection. This link also contains the code for how to handle the swipe events. In general, terms terms like fling and velocity are used to express the swipe event.
The general logic behind Swipe Event:
When a User swipes on the screen from Left to Right or Right to left the following happens: User first touches the screen (lets say first x coordinate is x1) holds, swaps then leaves the screen (lets say second x coordinate is x2)
So, now if (x2 > x1), it means that a left to right swipe has been done. and if (x2 < x1), it means that a right to left swipe has been done.
Similarly the tracking can be done in case of Up to Down or Down to Up.

- 1
- 1

- 2,353
- 4
- 28
- 46