21

I am working android game development and I am new in it. I want to develop a game which screen resolution free. It will be enough for me if i get to know that how to catch screen resolution of an android device using libgdx library. After some googling i read the following code on different sites, but it is not for libgdx.

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;

Except this i also used technique given on the following website that used "OrthographicCamera" class for solving the problem, and didn't work for me either.

http://www.java-gaming.org/index.php?topic=25685.0

How can i get the screen resolution or any way to adjust screen resolution for my game.

Daahrien
  • 10,190
  • 6
  • 39
  • 71
mfs
  • 3,984
  • 6
  • 32
  • 51

5 Answers5

100
Gdx.graphics.getWidth();
Gdx.graphics.getHeight();

:)

Daahrien
  • 10,190
  • 6
  • 39
  • 71
10

ok you can get the screen resolution by this

Gdx.graphics.getWidth();

//for screen width

Gdx.graphics.getHeight();

//for screen height

and if you want to make a screen resolution free game you can set the view port like this

float scrw=320;

float scrh=480;

scrw is viewport's width and scrh is viewport's height

and set the camera to

camera = new OrthographicCamera();
camera.setToOrtho(false, scrw, scrh);
camera.update(); 
Suraj
  • 607
  • 10
  • 16
sandeep kundliya
  • 963
  • 8
  • 13
8

Though there are some answers here on my question. This is what exactly worked for me. I had to add 'app' after 'Gdx' for it to work.

Gdx.app.graphics.getWidth(); 
Gdx.app.graphics.getHeight();
mfs
  • 3,984
  • 6
  • 32
  • 51
  • 8
    So you copy answers from others and accept your answer for brownie points? – Prakash Nadar Jul 10 '16 at 19:18
  • 3
    @Muhmmand, Lestat answered the question on 12th, your comment to that post shows that you got the solution from his answer. So no, accepting your own answer is naive. – Prakash Nadar Dec 30 '16 at 19:48
5

In the newer LibGDX release, the code is now

int width = Gdx.app.getGraphics().getWidth();
int height = Gdx.app.getGraphics().getHeight();
Hamzah Malik
  • 2,540
  • 3
  • 28
  • 46
-1
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();

works for me.

Avijit Biswas
  • 425
  • 5
  • 11