# Libgdx getting screen size

I'm working on libgdx project. I basically recreate my game which I coded in xna to make it available for android. Here's the problem android has many screen resolutions, and I want to know how to get them.

i tried Gdx.graphics.getWidth() or getHeight() but it gives me null pointer exceptions.

• Are you calling that before or after you new up a LwjglApplication? – bornander Jan 19 '15 at 14:32
• sorry i don't understand – Brsgamer Jan 19 '15 at 14:33
• The LwjglApplication is responsible for setting the static graphics property on the Gdx class, if you don't new up an instance of LwjglApplication you're going to have to set Gdx.graphics yourself. – bornander Jan 19 '15 at 14:35
• listen, i don't know libgdx very good. let me show you the code public Player(Vector2 position){ this.pos = position; speed = 500; } this is as you can see constructor for player class. Player p = new Player(new Vector2(224, 48)); and i have this in main class after class opens. i want to change that hardcoded numbers with screen width and height and modify it with basic - or + operators. – Brsgamer Jan 19 '15 at 14:38
• What platform are you building for? Desktop, Android or HTML? – bornander Jan 19 '15 at 14:42

camera = new OrthographicCamera();
camera.setToOrtho(false,800,400);


Above code will draw everything in your game on 800*400 virtual screen (in pixel) and fit, stretch, fill etc (see viewports ) on device's screen, independent of device screen size.

https://github.com/libgdx/libgdx/wiki/Orthographic-camera

https://github.com/libgdx/libgdx/wiki/Viewports

You dont need to specify android screen fix size if you want you Libgdx project on all android Devices. I have test this. Just specify screen size that you can get from Gdx.graphics.getWidth() and also for Height. You can give some fix value to Camera.. Test it on different android devices and this will work same on all.

once there's a valid screen loaded Gdx.graphics must have a valid width and height defined, i think you're just asking for something null that's not related

i.e. float[] size; size[0] = Gdx.graphics.getWidth(); ...

which it will give you a nice NPE