Android Gets the status bar, title bar, Actionbar, and screen height

Source: Internet
Author: User

I. Screen height and Width acquisition method

int screenwidth,screenheight;  WindowManager WindowManager = Getwindowmanager ();  Display display = Windowmanager.getdefaultdisplay ();  ScreenWidth = Display.getwidth ();  ScreenHeight = Display.getheight ();

  

Another kind

Displaymetrics dm = new Displaymetrics ();     Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);     int screenwidth = Dm.widthpixels;     

  

Second, the status bar height acquisition method

Rect frame = new rect (); GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (frame); int statusbarheight = Frame.top;

  

The above method is basically possible, but the following method is more

private int getstatusbarheight () {        class<?> c = null;        Object obj = null;        Field field = NULL;        int x = 0;        try {            c = class.forname ("Com.android.internal.r$dimen");            obj = C.newinstance ();            field = C.getfield ("Status_bar_height");            x = Integer.parseint (Field.get (obj). toString ());            Return Getresources (). getdimensionpixelsize (x);        } catch (Exception E1) {            log.d (TAG, "Get status bar height fail");            E1.printstacktrace ();            return;}    }

  

Third, get the height of the title bar

int contenttop = GetWindow (). Findviewbyid (window.id_android_content). GetTop ()//statusbarheight is the height of the above status bar int Titlebarheight = Contenttop-statusbarheight;

  

Iv. acquisition of Actionbar Heights

int actionbarheight = Getactionbar (). GetHeight ();

  

Note: If you start using the activity's OnCreate function, you need to put it in runnable, because the height of the control may not be determined at this time.

View Root; Root.post (New Runnable () {            @Override public            void Run () {                //to change body of the implemented methods use File | Settings | File Templates.                Getactivitycontentheight ();            }        });

  

Android Gets the status bar, title bar, Actionbar, and screen height

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.