1

I implemented gesture detection on my app and it works well on a activity.

Now, what i want to do is to be able to use those gesture detection in an AlertDialog. Indeed i have an activity with a Cancel button. When i click on this button, the user have to confirm his choice in an AlertDialog which have two button: Yes and No. However, in this AlertDialog, swipe gesture does not work and i am not able to choose between Yes or No because it only focus on the No button.

I want to implement Swipe gesture detection for this alert dialog. Here is what i did :

package com.example.recente;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.glass.touchpad.Gesture;
import com.google.android.glass.touchpad.GestureDetector;

public class ChoixUtilisateur extends Activity {


private GestureDetector mGestureDetector;
Button cancelno;
Button cancelyes;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGestureDetector = createGestureDetector(this);
    setContentView(R.layout.fragment_choix_utilisateur);
    Button execute = (Button)findViewById(R.id.execute);
    execute.setFocusable(true);
    execute.setFocusableInTouchMode(true);///add this line
    execute.requestFocus();

}



private GestureDetector createGestureDetector(Context context) {
    GestureDetector gestureDetector = new GestureDetector(context);
    //Create a base listener for generic gestures

    gestureDetector.setBaseListener( new GestureDetector.BaseListener() {

        @Override
        public boolean onGesture(Gesture gesture) {
            Button cancel = (Button)findViewById(R.id.cancel);

            AlertDialog.Builder cancelbutton = new          AlertDialog.Builder(ChoixUtilisateur.this);

            if (gesture == Gesture.TAP) {
                // do something on tap
                 if (cancel.hasFocus()){



            //              .getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)
                            cancelbutton.setTitle("Confirm");  
                            cancelbutton.setMessage("Do you really want to proceed ?");  
                            cancelbutton.setPositiveButton("Yes",new    DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int i){
                                    dialog.cancel();
                                    Intent intent = new Intent(ChoixUtilisateur.this,Cancel.class);
                                    startActivity(intent);
                                    finish();
                                }
                            });  
                            cancelbutton.setNegativeButton("No", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int i){
                                    dialog.cancel();
                                }
                            });
                            cancelbutton.setCancelable(false) ;
                            cancelbutton.create();  
                            AlertDialog dialog = cancelbutton.create(); 
                            cancelno = dialog.getButton(AlertDialog.BUTTON_POSITIVE); 
                            cancelyes = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
                            cancelbutton.show();  


                }
                else{

                }



                return true;
            } else if (gesture == Gesture.TWO_TAP) {
                // do something on two finger tap
                return true;
            } else if (gesture == Gesture.SWIPE_RIGHT) {
                // do something on right (forward) swipe


                 if(cancelno.hasFocus()){
                    cancelyes.setFocusable(true);
                    cancelyes.setFocusableInTouchMode(true);///add this line
                    cancelyes.requestFocus();
                }
                else{
                    cancelno.setFocusable(true);
                    cancelno.setFocusableInTouchMode(true);///add this line
                    cancelno.requestFocus();
                }

                return true;
            } else if (gesture == Gesture.SWIPE_LEFT) {
                // do something on left (backwards) swipe
                if(cancelno.hasFocus()){
                    cancelyes.setFocusable(true);
                    cancelyes.setFocusableInTouchMode(true);///add this line
                    cancelyes.requestFocus();
                }
                else{
                    cancelno.setFocusable(true);
                    cancelno.setFocusableInTouchMode(true);///add this line
                    cancelno.requestFocus();
                }
                return true;
            }
            return false;
        }
    });


    return gestureDetector;
}


}

1 Answer 1

3

I am not sure that using alert dialog is best practice with Google Glass, use the standard cards or if you want more customization you can use simple activities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.