Android Get screen Info

Source: Internet
Author: User


Get screen info: status bar height, content size


public void Getscreeninfo (final activity activity) {        final View v = Activity.getwindow (). Findviewbyid (window.id_ android_content);        V.getviewtreeobserver (). Addonpredrawlistener (New Viewtreeobserver.onpredrawlistener () {            @Override            public Boolean Onpredraw () {                rect frame = new Rect ();                Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);                int statusbarheight = frame.top;                V.getviewtreeobserver (). Removeonpredrawlistener (this);                LOG.E ("", "Screeninfo===contentview===height:" + v.getheight () + "  Width:" + v.getwidth () + "Top:" + v.gettop () + "
   left: "+ v.getleft () +"   Bottom: "+v.getbottom () +"   status bar Height: "+statusbarheight);                return true;            }        );    }

Other tool classes: Get screen size, density, horizontal screen vertical screen


private static int widthpixels = 0;    private static int heightpixels = 0;    private static float density = 0;        public static Displaymetrics Getmetrics (context context) {Displaymetrics metrics = new Displaymetrics ();        Getwindowmanager (context). Getdefaultdisplay (). Getmetrics (metrics);    return metrics; public static String Getorientation (context context) {switch (Getwindowmanager (context). Getdefaultdisplay (). g            Etrotation ()) {case Surface.ROTATION_0:return "portrait";            Case Surface.ROTATION_90:return "Landscape";            Case Surface.ROTATION_180:return "Reverse portrait";        Default:return "Reverse Landscape"; }} public static WindowManager Getwindowmanager (context context) {return (WindowManager) context.getsystems    Ervice (Context.window_service);   } public static int getwidth (context context) {if (Widthpixels = = 0) {         Widthpixels = getmetrics (context). Widthpixels;    } return widthpixels; } public static int getheight (context context) {if (Heightpixels = = 0) {Viewconfiguration.get (conte            XT). Haspermanentmenukey ();        Heightpixels = getmetrics (context). Heightpixels;    } return heightpixels; public static float Getdensity (context context) {if (density = = 0) {density = getmetrics (context)        . density;    } return density; }



Android Get screen Info

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.