Search code examples
dialogcodenameone

Codename One refresh dialog


I have an Android app where I want to refresh the layout of an open dialog.

The dialog has a container with a button and a label in it, and when the dialog is open, I want to change the text inside the label and the position of the container on the screen through the button action.

The text inside the label changes, but the position of the container does not. It there any way to achieve this?

Many thanks in advance ... here is my code:

Button buttonDialog = new Button("Yes");
Container container = new Container (new BorderLayout ());
SpanLabel spanLabel = new SpanLabel("One");
Dialog dialogStart = new Dialog("Button", new BoxLayout (BoxLayout.Y_Axis));

dialogStart.getTitleStyle().setFgColor(0xcccccc);
dialogStart.getTitleStyle().setBorder(Border.createEmpty());
dialogStart.getTitleStyle().setFont(Font.createSystemFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE));
dialogStart.setDisposeWhenPointerOutOfBounds(false);
dialogStart.setScrollable(false);
dialogStart.setScrollVisible(false);
dialogStart.setLayout(new BorderLayout());

container.add(BorderLayout.CENTER, spanLabel);
container.add(BorderLayout.SOUTH, buttonDialog);

dialogStart.add(dialogStart);

buttonDialog.addActionListener(l->{

    spanLabel.setText("Two");
    containerDialog.getAllStyles().setMargin(1000,100,500,150);  // does not work 
    dialog.repaint();                 // does not work 
    dialog.refreshTheme();            // does not work      
});

Solution

  • Remove all of these hacks. Use growOrShrink() which is built exactly for this use case.

    If you need the dialog to be in a precise size/location. Just disable the dialog transition animation out (set to empty not null) and dispose it. Then show a new dialog without an "in" animation.