# Difference between Gdx.graphics.getWidth() and camera.viewportWidth

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

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.

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