Thursday, September 29, 2011

Disable Dismiss command in Nokia alert

Did you ever had this problem that there is an unexpected 'dismiss' command in nokia alerts while writing a j2me app?

I found this very annoying. I had an app where I wanted to show an infinite progress alert, and I didnt want to have any cancel button there (it was a short save operation).

I tried adding a command with space, empty string to replace the default dismiss command, but did not work. Finally the solution that worked for me was:
  • Add a command with string "\u200b" (zero width space)
  • add an empty commandlistener
Code snippet:



/**
* Creates and returns an Alert with the message. 
* If noOptionForUser/code> is set as true, 
* Alert will have it's own CommandHandler to consume all
* user events, Alert will have an 'invisible' Command to remove DISMISS_COMMAND
* and timeout will be set as Alert.FOREVER. If noOptionForUser/code> is false,
* it will just add the gauge with indefinite progress alert.
* @param message message to be displayed
* @param noOptionForUser if true, user is not given any option to choose. 
* Alert will have it's own commandHandler to consume all events.
* @return an Alert object with an INDEFINITE mode gauge attached to it
*/
public static Alert getProgressAlert(String message, boolean noOptionForUser)
{
  Alert alert = new Alert(null,message, null, AlertType.INFO);
  Gauge gauge = new Gauge(null, false, Gauge.INDEFINITE,
                                           Gauge.CONTINUOUS_RUNNING);
  alert.setIndicator(gauge);
  if(noOptionForUser) {
     alert.setCommandListener(new CommandListener() {
           public void commandAction(Command cmd, Displayable disp) {
           }
     });
     alert.setTimeout(Alert.FOREVER);
     alert.addCommand(new Command("\u200B", Command.OK, 1));//we dont like to see the DISMISS command, so adding an invisible command
  }
  return alert;
}

1 comment: