Android learning notes: getting mobile phone screen size

Source: Internet
Author: User

The screen size of Android mobile phones has always been a headache for developers. Because the screen sizes used by mobile phone manufacturers are different, the presentation and layout of user UI interfaces are naturally different. Therefore, when developing Android mobile appsProgramIn addition to understanding the underlying API, the most important thing is the definition of screen resolution.

Android can be set to adjust the zoom ratio with the window size, but even so, the mobile phone program designer must still clearly know the border of the mobile phone screen to avoid Layout) deformation Problem. In Android, only a few lines are required.CodeThe cell phone screen resolution can be obtained. The key is the displaymetrics application.

The displaymetrics class is directly inherited from the object class and stored in the Android. util package. Displaymetrics objects record some common information, including the display information, size, dimension, and font, as shown in the following table:

 

Static int Default_density
The reference density used throughout the system.
Float Density
The logical density of the display.
Int Heightpixels
The absolute height of the display in pixels.
Float Scaleddensity
A scaling factor for fonts displayed on the display.
Int Widthpixels
The absolute width of the display in pixels.
Float Xdpi
The exact physical pixels per inch of the screen in the X dimension.
Float Ydpi
The exact physical pixels per inch of the screen in the Y dimension.

 

It is worth mentioning that widthpixels and heightpixels record the screen width and height of the mobile phone. We can use these two values to get the screen resolution of the mobile phone.Note that the pixels here refer to absolute (absolute) pixels rather than relative pixels.

 

The following code gets the screen resolution:

Public class mainactivity extends activity {private textview text = NULL; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); super. setcontentview (R. layout. activity_main); this. TEXT = (textview) super. findviewbyid (R. id. text); displaymetrics dm = new displaymetrics (); super. getwindowmanager (). getdefadisplay display (). getmetrics (DM); string stropt = "cell phone screen resolution:" + DM. widthpixels + "*" + DM. heightpixels; this. text. settext (stropt );}}

The layout file is very simple. Just use a textview component:

<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: paddingbottom = "@ dimen/activity_vertical_margin" Android: paddingleft = "@ dimen/plugin" Android: paddingright = "@ dimen/plugin" Android: paddingtop = "@ dimen/plugin" tools: context = ". mainactivity "> <textview Android: Id =" @ + ID/text "Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "/> </relativelayout>

Program running effect:

 

 

 

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.