Search code examples
libgdxscene2d

Textbutton not working and scaling issues


I've created a table with scened2d that contains buttons, but i have many issues with thoses buttons.

First issue is scaling, whenever i try to set a button's with and height an issue appear when i set it with :

table.add(btn).with(100).height(30);

or

btnStyle.up.setMinWidth(80);
btnStyle.up.setMinHeight(40);
btnStyle.down.setMinWidth(80);
btnStyle.down.setMinHeight(40);

the button is correctly scaled but the background is acting weird

Buttons][1

But if i don't set their with and height, their background are normal but the buttons are way too big

Second issue, the buttons don't "work" as i can't click on then (nothing change) here is the code

// style
final Skin skin = new Skin(Gdx.files.internal("skin/glassy-ui.json"));
final BitmapFont font = skin.getFont("font");
final TextButton.TextButtonStyle btnStyle = new TextButton.TextButtonStyle();
btnStyle.font = font;
btnStyle.up = skin.getDrawable("button");
btnStyle.down = skin.getDrawable("button-down");
btnStyle.up.setMinWidth(80);
btnStyle.up.setMinHeight(40);
btnStyle.down.setMinWidth(80);
btnStyle.down.setMinHeight(40);

// buttons
Textbutton btn = new TextButton("some text", btnStyle);

how can i scale my buttons correctly and what could cause the buttons to stop working ?

(there is already a question about buttons scaling but it didn't helped much)


Solution

  • For getting the clicks on the button, you have to add a ChangeListener.

    The assets you are using as button background are too big, or their padding is too big. Use the Skin Composer to get a preview for your skin.