Blog Archives


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.