user3740097
user3740097

Reputation:

Could not set scene2d.ui.Dialog size

I'm trying to display a dialog in the middle of the screen. But I couldn't change the size of the dialog with setWidth() or setHeight(). I have the following code:

private void showDialog() {
    Window.WindowStyle dialogStyle = new Window.WindowStyle();
    dialogStyle.background = new TextureRegionDrawable(new TextureRegion(dialog_bg));
    dialogStyle.titleFont = gameFont;
    Dialog dialog = new Dialog("Test Dialog", dialogStyle);
    dialog.setWidth(200);   // will be ignored
    dialog.show(stage);
}

Any ideas?

Upvotes: 4

Views: 1177

Answers (1)

AndreGraveler
AndreGraveler

Reputation: 497

Just call your changes to the size after you call dialog.show(). In this case, I am using a Stage with a FitViewport, so I scale down my actors so that they fit. When I adjust their size, I have to factor in the scale.

public void create(Dialog dialog, float scale){
    dialog.setScale(scale);
    dialog.setMovable(false);

    dialog.text("Are you sure you want to yada yada?");
    dialog.button("Yes", true); //sends "true" as the result
    dialog.button("No", false); //sends "false" as the result
}

public void show(Stage st) {
    dialog.show(st); //pack() is called internally
    dialog.setWidth(worldWidth/scale); //we override changes made by pack()
    dialog.setY(Math.round((st.getHeight() - dialog.getHeight()) / 2)); //we override changes made by pack()
}

Upvotes: 2

Related Questions