Resizing TextButton LibGDX

724 views Asked by At

I'm kinda new to LibGDX. I'm just learning the ropes. Trying to get a basic TextButton to display at a reasonable size on screen. Working exclusively on Android IDE (AIDE). Using only default skins and what-not, I've tried to set width and height as well as setScale() but only the text size seems to increase on the command. What am I doing wrong?

https://oi1161.photobucket.com/albums/q501/StudioGilliam/Screenshot_20190423-184819_LibGDXButtonsTest.jpg

public class MyGdxGame implements ApplicationListener
{
   private Stage stage;
   private Table table;
   private TextButton btn;

   @Override
   public void create()
   {
      stage = new Stage(new ScreenViewport());
      Gdx.input.setInputProcessor(stage);

      table = new Table();
      table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

      TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
      style.font = new BitmapFont();

      btn = new TextButton("Button", style);
      btn.pad(20);
      btn.setTransform(true);
      btn.setScale(5.0f);
      btn.setTouchable(Touchable.enabled);

      table.add(btn);
      table.debug();

      stage.addActor(table);

   }

   @Override
   public void render()
   {        
    Gdx.gl.glClearColor(0, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      stage.act();
      stage.draw();
   }

   @Override
   public void dispose()
   {
      stage.dispose();
   }

   @Override
   public void resize(int width, int height){}

   @Override
   public void pause(){}

   @Override
   public void resume(){}
}

EDIT: I realised that parent objects like stages and tables control the properties of their children, so I added:

table.add(btn).width(300).height(100);

And now it looks better but the text is way off centre...

https://oi1161.photobucket.com/albums/q501/StudioGilliam/Screenshot_20190423-191104_LibGDXButtonsTest.jpg

1

There are 1 answers

0
MrStahlfelge On

On a Textbutton, you should use setFontScale() and pack() to resize the button to the font size.

However, for supporting different screen sizes without any own coding, consider using FitViewport/ExtendViewport