LibGDX resizing window

So I’m making a game with LibGDX, and am pretty far into the development. I’m not using scene2d right now, and don’t plan on switching over because it seems like too much work for little gain (I will use scene2d for future projects). So right now all of my UI is done with BitmapFonts manually positioned around as text and buttons. I noticed that when I resize my window and change screens, the positioning of my UI gets messed up. I’m positioning everything relative to Gdx.graphics.getWidth() and Gdx.graphics.getHeight() (example: buttonX = Gdx.graphics.getWidth() - 100). So what is a good way to position my UI so that it can be resized and still retain the same layout, without tapping into scene2d?

What do you mean by a good way to position UI so that it can be re-sized and still retain same layout? Didn’t you solve your problem already? (Gdx.graphics.getWidth() - 100)

Using a specific number of pixels to set the position will not scale very well.

To illustrate the problem of “Gdx.graphics.getWidth() - 100”:

  • If the width was 400, then subtracting 100 would move it 1/4th of the screen width.
  • If the width was 800, then subtracting 100 would move it 1/8th of the screen width.

This is why your UI gets messed up because the button’s relative position on the screen changes. To fix that, try setting the position based on a percentage of the screen size. Something like:

button1X = Gdx.graphics.getWidth() * 0.1; // 10% of screen width
button2X = Gdx.graphics.getWidth() * 0.5; // 50% of screen width
button3X = Gdx.graphics.getWidth() * 0.9; // 90% of screen width
etc...

These three buttons will always be positioned at 10%, 50%, and 90% of the screen width, so no mater what the screen size is they will stay in their relative positions.

Also, I did not take into account width of the button, so depending on how you want to position your buttons you may need to take that into account also.