8

So I'm trying to use a gesture overlay view in android to make a "swipe" action. So that when the user "swipes" left it executes certain code and when they swipe right it executes other code. I tried declairing the gestureoverlay like this:

GestureOverlayView gest = (GestureOverlayView) findViewById(R.id.hatgest);

But then i don't know where to go from there and i cant find anything helpful in the dev guide or online. For a button i would normally use an "onclicklistener" how would i do this with the gesture overlay? Does anyone have any examples of code that i can reference? Thanks

Peter
  • 5,071
  • 24
  • 79
  • 115

2 Answers2

11

Firstly make you custom gestures from gesture builder. Gesture builder app comes in the sdk. Put the file created from gesture builder app into raw folder of the application you are about to use these gestures. You can also get help from documentation

  public class YourClass extends Activity implements OnGesturePerformedListener {

    private GestureLibrary mLibrary;
    mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
    if (!mLibrary.load()) {
      finish();
    }

    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
    gestures.addOnGesturePerformedListener(this);

    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
      ArrayList < Prediction > predictions = mLibrary.recognize(gesture);
      Log.v("performed", "performed");

      // We want at least one prediction
      if (predictions.size() > 0) {
        Prediction prediction = predictions.get(0);

        // We want at least some confidence in the result
        if (prediction.score > 1.0) {
          if (prediction.name.equalsIgnorecase("right")) {
            //do you thing here//
          }
        }
      }
    }
  }
Scorpius
  • 999
  • 1
  • 10
  • 22
ozmank
  • 763
  • 1
  • 8
  • 23
  • 4
    I'm trying to implement this right now, but I am getting na error for `R.raw.gestures`, `raw cannot be resolved or is not a field.` Anyone have any ideas? I'm working in version 10 of the SDK – JuiCe Dec 28 '12 at 17:14
  • 1
    @JuiCe you should first create a gesture (usually using gesture builder in emulator) and then move the created gesture (find it in sdcard of emulator) to the raw folder of your project – frogatto Jan 30 '14 at 11:55
-1

Apparently GestureOverlayViews can have multiple onGestureListeners.

Check out the method addOnGestureListener() and addOnGesturePerformedListener().

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
Ian
  • 3,500
  • 1
  • 24
  • 25