Android

How To Get Screen Dimensions Programmatically In Android

Get Screen Dimensions Programmatically In Android In Pixels

The following source code shows how to get dimensions or screen sizes of the Android device your application is running on:

Create an activity class called MainActivity.java and write the following code into onCreate method

Display display = getWindowManager().getDefaultDisplay();
if (android.os.Build.VERSION.SDK_INT >= 13) {


Point size = new Point();
display.getSize(size);
int width = size.x;  // width of the screen
int height = size.y;  // height of the screen

} else {  // Deprecated method
 
int width = display.getWidth();   
int height = display.getHeight();   
}

In dp unit

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
float widthDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, displaymetrics.widthPixels, getResources().getDisplayMetrics());
float heightDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, displaymetrics.heightPixels, getResources().getDisplayMetrics());
About author

Rojer is a programmer by profession, but he likes to research new things and is also interested in writing. Devdeeds is his blog, where he writes all the blog posts related to technology, gadgets, mobile apps, games, and related content.

Leave a Reply

Your email address will not be published. Required fields are marked *