How to obtain the width and height of a device in Android

Source: Internet
Author: User

Before Android 4.0, you can use the following method to obtain the screen width and height:

DisplayMetrics dm = new DisplayMetrics ();

GetWindowManager (). getDefaultDisplay (). getMetrics (dm );

Int mScreenW = dm. widthPixels; // obtain the width.

Int mScreenH = dm. heightPixels; // get the height

In the above method, before 4.0, the total width and total height of the screen are obtained, including the height of the status bar and the height of the navigation bar, such as a 480*800 device, the values obtained through the above method are 480 and 800, but the height obtained by the above method after 4.0 is the remaining height values except the navigation bar height, for example, if the height of the navigation bar is 48, the value of mScreenH obtained after 4.0 is 480-48 = 432.


Therefore, if you want to get the total height of the screen after 4.0, you need to use the following method:

Display display = getWindowManager (). getDefaultDisplay ();
Point point = new Point ();
Display. getRealSize (point );
Int mScreenH = point. y;

Int mScreenW = point. x;

The obtained width and height are 480 and 800.

Note: We are a car machine, so the size is the size of the horizontal screen.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.