Skip to content Skip to sidebar Skip to footer

Calculate Screen Size In Android

I have traveled all over the internet looking for a way to do something that I thought would be very basic. Bottom line is: I have an android UI that I have designed. It consists o

Solution 1:

You can use the WindowManager to get the screen size. In your activity:

getWindow().getWindowManager().getDefaultDisplay().getWidth();

or getHeight() in your case. Once you have that, just do your math, and then:

ViewGroup.LayoutParams buttonLayout = yourButton.getLayoutParams();

buttonLayout.height = 42; // Set your height

buttonLayout.y = 42; //Set distance from top of the screen

and so on. Once done:

yourButton.setLayoutParams(buttonLayout);

Let me know if this works.


Solution 2:

If you design your interface using dip (or dp) and sp units (for fonts) you can let Android do the scaling for you. Although maybe not pixel perfect on every device, your app will certainly look the same on many devices in the correct scale.

Supporting multiple screens on http://d.android.com describes this...


Solution 3:

I am a bit new to android, although, in the game I am creating I found that it is easiest to allow android to determine the dpi(dots per inch) of the screen and choose my images accordingly.

For example, my games res folder has folders listed as drawable, drawable-hdpi, and more. These folder are here for you to put the corresponding images in for the type of screen.

In the android manifest you can list what screen dpi you support and those folders will be used correctly.

Hope this helps!

EDIT: I would definitely check out this doc page

http://developer.android.com/guide/practices/screens_support.html


Post a Comment for "Calculate Screen Size In Android"