I am a newbie in game development and I am developing an android game using Libgdx on intelliJ IDE. I want to understand the difference between Gdx.graphics.getWidth() and camera.viewportWidth . As I want the size of everything (example buttons) in my game to be by percentage of width and height of the screen, so what should I use for that Gdx.graphics.getWidth() or camera.viewportWidth ?


1 Answer 1


Gdx.graphics.getWidth() gives you the width in pixels of the either the window (in desktop mode) or the screen (for IOS or Android).

viewportWidth on the Camera is the span in world-units that the camera can see.

If you set every size up to be a percentage of the screen size then you can use any of the two.

Based on a guess of what you're trying to achieve I think a ScreenViewport would work well for you, but note that you might run into issues with aspect ratio as not all devices have screens in the same aspect ratio.

There's good documentation about viewports available at the libGDX wiki.

  • 1
    \$\begingroup\$ Thanks for reminding me about aspect ratio . Can you please tell me what can I use to maintain the aspect ratio ? \$\endgroup\$
    – ZaidRehman
    Mar 4, 2017 at 20:35
  • \$\begingroup\$ @ZaidQureshi click the link in his post. It talks about viewports and maintaining aspect ratio. \$\endgroup\$
    – Charanor
    Mar 5, 2017 at 11:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .