Search code examples
dialogpopupcodenameone

Codename One close PopUp dialog only when I tap the screen


I have an Android with many explicative popup dialogs.

Those I want to close only if a close button inside is pressed, not when I tap on the screen outside the dialog.

I am using setDisposeWhenPointerOutOfBounds set to false, but this only seems to work for dialogs that are not the popup style.

Is there a way to achieve this behaviour also for the popup dialogs?

Many thanks in advance.

Here is my code:

public void dialogTest () {
    
    Dialog dialog = new Dialog("Test");
    dialog.add(new Button ("Test")).add(new Button("TestTwo"));
    dialog.setDisposeWhenPointerOutOfBounds(false);  // flag is set to false, but dialog closes anyway when I tap on the screen
    dialog.showPopupDialog(buttonTest);
    
}

Solution

  • @rainer Yes, it's possible. First, however, a brief explanation. For reasons I don't know, in the Dialog.java file on line 1176, disposeWhenPointerOutOfBounds = true; was imposed. You can check it yourself: https://github.com/codenameone/CodenameOne/blob/1ad583bae9e05954c6bff852649d3b6d8264414b/CodenameOne/src/com/codename1/ui/Dialog.java#L1176

    That's why dialog.setDisposeWhenPointerOutOfBounds(false); doesn't work for you, because it was designed to work only with normal Dialogs and not with Dialogs popups.

    This behavior is also specified in Javadoc, so it seems an intentional choice, although I don't know why and, in particular, I don't know if there are differences between Android and iOS on this point, since they have different default ways to create popup dialogs (and maybe the problem is right here).

    Said that, the solution is very simple: it works in the Simulator, I'll let you try if it works on real devices too. Simply override the onShow method in this way:

    Dialog d = new Dialog(new BorderLayout()){
        @Override
        protected void onShow() {
             this.setDisposeWhenPointerOutOfBounds(false);
        }
    };
    [...]
    d.showPopupDialog(buttonTest);