Android get the screen height and width of the implementation method _android

Source: Internet
Author: User

The example in this article describes how Android gets the screen height and width. Share to everyone for your reference. The specific analysis is as follows:

We need to get the physical dimensions of the Android phone or pad screen to facilitate the design of the interface or the implementation of other functions. Here's how to get the physical dimensions of a screen

The following code gets the size of the screen.
In the OnCreate method of an activity, write the following code:

Copy Code code as follows:
Displaymetrics metric = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (Metric);
int width = metric.widthpixels; Screen width (in pixels)
int height = metric.heightpixels; Screen height (in pixels)
float density = metric.density; Screen Density (0.75/1.0/1.5)
int densitydpi = metric.densitydpi; Screen density dpi (120/160/240)

However, it should be noted that on a low density small screen mobile phone, only the above code is not able to get the correct size. For example, a low-density cell phone with a 240x320 pixel, if you run the code above, the screen size you get is 320x427. As a result, the study found that without multiple resolution support, the Android system would convert 240x320 's low-density (120) dimensions to Medium-density (160) dimensions, which would greatly affect the coding of the program. Therefore, in the project Androidmanifest.xml file, add the Supports-screens node, the specific contents are as follows:

Copy Code code as follows:
<supports-screens
Android:smallscreens= "true"
Android:normalscreens= "true"
Android:largescreens= "true"
Android:resizeable= "true"
Android:anydensity= "true"/>

In this way, the current Android program supports multiple resolutions, so you can get the correct physical size.

Copy Code code as follows:
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.DisplayMetrics;
Import Android.widget.TextView;
public class Textcanvasactivity extends activity {
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (This) (new MyView);

Defining Displaymetrics Objects
Setcontentview (R.layout.main);
Displaymetrics dm = new Displaymetrics ();
Get Window Properties
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);

Width of the window
int screenwidth = Dm.widthpixels;

Window height
int screenheight = Dm.heightpixels;
TextView TextView = (TextView) Findviewbyid (R.ID.TV1);
Textview.settext ("screen width:" + screenwidth + "\ n Screen Height:" + screenheight);
}
}

I hope this article will help you with your Android program.

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.