How to obtain the screen width of an Android phone

Source: Internet
Author: User

There are two ways to get the screen width of an Android phone: one is to get the screen width through the View method, and the other is to get the screen width through the Display object.

Below this article introduces very detailed, interested friends can look at the past, the original address is as follows: http://www.iteye.com/topic/828830 online there are other articles, interested friends can try on their own.

Here I will share with you how to obtain the screen width and height through DisplayMetrics parameters. I will post the source code below and share it with you. I will not explain it too much because I have clearly read and written it.

Import android. app. activity; import android. content. context; import android. OS. bundle; import android. util. displaymetrics; import android. widget. textview; public class displayactivity extends activity {/* First declares a text control for displaying the obtained width and height */private textview TV = NULL; /** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // load the layout file setcontentview (R. layout. main); // find the corresponding control TV = (textview) findviewbyid (R. id. TV); // to obtain the screen width and height parameters, you must first declare a displaymetrics object. The screen width and height attributes are stored in this object displaymetrics dm = new displaymetrics (); // obtain the window manager, obtain the current window, call getdefadisplay display (), write some information about the screen into the DM object, and finally get getwindowmanager () through getmetrics (DM (). getdefadisplay display (). getmetrics (DM); int WDIP = px2dip (getapplicationcontext (), getapplicationcontext (). getresources (). getdisplaymetrics (). widthpixels); int HDIP = px2dip (getapplicationcontext (), getapplicationcontext (). getresources (). getdisplaymetrics (). heightpixels); // print the obtained width and height TV. settext ("densitydpi:" + DM. densitydpi + "\ n" + "density:" + DM. density + "\ n" + "scaleddensity:" + DM. scaleddensity + "\ n" + "heightpixels (the absolute height of the display in pixels .): \ n "+ DM. heightpixels + "\ n" + "widthpixels (the absolute width of the display in pixels .): \ n "+ DM. widthpixels + "\ n" + "xdpi (the exact physical pixels per inch of the screen in the X dimension): \ n" + DM. xdpi + "\ n" + "ydpi (the exact physical pixels per inch of the screen in the Y dimension): \ n" + DM. ydpi + "\ n" + "WDIP:" + WDIP + "\ n" + "HDIP:" + HDIP + "\ n ");} /*** change value from pix to dip ** @ Param context * @ Param pxvalue * @ return */public final static int px2dip (context, float pxvalue) {final float scale = context. getresources (). getdisplaymetrics (). density; Return (INT) (pxvalue/scale + 0.5f );}}
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.