How Android Gets the device screen size

Source: Internet
Author: User

Get through WindowManager

Displaymetrics dm = new Displaymetrics ();

Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);

System.out.println ("Heigth:" + dm.heightpixels);

System.out.println ("width:" + dm.widthpixels);

Access through resources

Displaymetrics dm2 = Getresources (). Getdisplaymetrics ();

System.out.println ("heigth2:" + dm2.heightpixels);

System.out.println ("width2:" + dm2.widthpixels);

Get the default resolution of the screen

Display display = Getwindowmanager (). Getdefaultdisplay ();

System.out.println ("Width-display:" + display.getwidth ());

System.out.println ("Heigth-display:" + display.getheight ());

As you can see, the first and third methods use the Getwindowmanager () method, and the second method does not use Getwindowmanager (). Some netizens said that after writing Getwindowmanager () Eclipse will error, said there is no Getwindowmanager () This method, what is the reason? Because the Getwindowmanager () method is in class activity, if you write your own class that does not inherit from the class activity, you must write code in this class without using the Getwindowmanager () method. So when you write your own class that doesn't inherit from the class activity, you can only use the second method to get the size of your phone screen.

How Android Gets the device screen size

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.