19

What I want is to have an options menu where the user can choose to navigate the menu between:

  1. Touching a button and then pressing down on the trackball to select it
  2. Drawing predefined gestures from Gestures Builder

As it stands now, I have created my buttons with OnClickListener and the gestures with GestureOverlayView. Then I select starting a new Activity depending on whether the using pressed a button or executed a gesture. However, when I attempt to draw a gesture, it is not picked up. Only pressing the buttons is recognized. The following is my code:

public class Menu extends Activity implements OnClickListener, OnGesturePerformedListener {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //create TextToSpeech
    myTTS = new TextToSpeech(this, this);
    myTTS.setLanguage(Locale.US);

    //create Gestures
    mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
    if (!mLibrary.load()) {
     finish();
    }

    // Set up click listeners for all the buttons.
    View playButton = findViewById(R.id.play_button);
    playButton.setOnClickListener(this);
    View instructionsButton = findViewById(R.id.instructions_button);
    instructionsButton.setOnClickListener(this);
    View modeButton = findViewById(R.id.mode_button);
    modeButton.setOnClickListener(this);
    View statsButton = findViewById(R.id.stats_button);
    statsButton.setOnClickListener(this);
    View exitButton = findViewById(R.id.exit_button);
    exitButton.setOnClickListener(this);

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

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

 // 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) {
  // Show the gesture
   Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
   //User drew symbol for PLAY
    if (prediction.name.equals("Play")) {
       myTTS.shutdown();
       //connect to game
      // User drew symbol for INSTRUCTIONS
     } else if (prediction.name.equals("Instructions")) {
           myTTS.shutdown();
           startActivity(new Intent(this, Instructions.class));
       // User drew symbol for MODE
   } else if (prediction.name.equals("Mode")){
      myTTS.shutdown();
      startActivity(new Intent(this, Mode.class));
       // User drew symbol to QUIT
  } else {
      finish();
   }
  }
 }
}

@Override
    public void onClick(View v) {
      switch (v.getId()){
      case R.id.instructions_button:
         startActivity(new Intent(this, Instructions.class));
         break;
      case R.id.mode_button:
         startActivity(new Intent(this, Mode.class));
         break;
      case R.id.exit_button:
         finish();
         break;
  }  
}

Any suggestions would be greatly appreciated!

Macarse
  • 91,829
  • 44
  • 175
  • 230
user264427
  • 221
  • 1
  • 3
  • Is the gesture working? is `onGesturePerformed` being called? – Macarse Mar 13 '11 at 22:33
  • What's your layout look like? Chances are that your gesture overlay is not where the user is drawing gestures. – Dave MacLean Mar 20 '11 at 17:06
  • gestures dont seem to work very well over many ui controls. i have had to revert to using a dedicated "slide me" control, where the user slides over it and it says "gesture detected". it eats up real estate however. see my app CueBrain to see what it looks like. – swinefeaster Mar 27 '11 at 18:51
  • Half of the information is missing in this question: please show your xml layout. – olivierg Apr 07 '11 at 12:33
  • This may seem to be useful to you: http://developer.android.com/resources/articles/gestures.html – Hassassin Apr 10 '11 at 09:31

1 Answers1

2

If I understood correctly, you want to perform both click action and gesture action on the same view, I am not sure how to achieve this, however, we can have invisible view behind the buttons to handle gesture and buttons on top will handle the click events as usual.

Default Calculator application uses this approach to swap between normal mode and advanced mode.

Calculator Source

Refer, com.android.calculator2.PanelSwitcher - for gesture events com.android.calculator2.EventListener - for click events

hope it helps.

Riyaz Mohammed Ibrahim
  • 9,505
  • 5
  • 26
  • 33