1 Step to get device with and height in Android



In Android project, we often have to get the height and width of a device, so that we can change our size of our ui to adjust to different devices, In android, to get these two information is simple, you can do one step.

Step 1: Use DisplayMetrics to get width and height of a device

In android, wen can use class DisplayMetrics to get the with and height of a device, code like this:


private void getScreenSize(){
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels; // width of screen
int screenHeight = dm.heightPixels;// height of screen
}

From code above, the screenWidth and screenHeight are what you want.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>