Question

I have read in the GDK documentation that there are menu options which when get selected provide user with Grace period to cancel there action by swiping down.

Like the delete option which you get when you tap a card in Glass timeline.

I was wondering how can i implement such a menu item which has grace period ?

Was it helpful?

Solution

There's no built in solution at the moment, which is a bit disappointing. Hopefully with the next update they'll add a bunch of basic functionality that should have always been in the GDK.

See https://github.com/pif/glass-progress-bar

That library is pretty feature complete and will let you do everything you want.

OTHER TIPS

Something like the following should do it:

public static Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());
private Integer pendingAction;
private Boolean isActionPending = false;
private final Long TIMEOUT = 2000; //2 seconds
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (isActionPending) {
       //TODO: better error handling
       return false;
     }
     isActionPending = true; 
    pendingAction = item.getItemId();
    new Thread( new Runnable() {
            @Override
            public void run() {
                Thread.sleep(TIMEOUT);
                MAIN_HANDLER.post(
                    new Runnable() {
                    @Override
                    public void run() {
                        onDoPendingAction();                                    
                    }
                    }
                );
                }
}).start();

}
public void onDoPendingAction() {
   if (!isPending ) {
     return; //event was canceled
   }
   switch(pendingAction) {
     case R.id.some_menu_action: 
        //Whatever you would do.
        break;
    /* other actions */
   }
   //TODO: something to indicate teh action finished.
}
public void cancelPendingAction() {
  if (isPending) {
     isPending = false;
     //TODO: something to indicate it canceled.  
  } 
}

Add a gesture detector to capture the TWO_SWIPE_DOWN event and call cancelPendingAction(); if you detect it. Get gesture detector code here.

https://developers.google.com/glass/develop/gdk/input/touch

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top