Get Android screen size, control size, status bar/notification bar height, navigation bar height

Source: Internet
Author: User

1. Get the Android screen size

We can get the size of the screen through the GetSize () method

new Point();display.getSize(size);int width = size.x;int height = size.y;

If not in activity, you cannot use Getwindowmanager (), you can use Window_service to get a default display

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);Display display = wm.getDefaultDisplay();

The GetSize () method was added after API 13, which we need to do before API 13

int width = display.getWidth();  // 已经过时int height = display.getHeight();  // 已经过时

In order to fit all the equipment, we should write this

if13) {            display = getWindowManager().getDefaultDisplay();            new Point();            display.getSize(size);            width = size.x;            height = size.y;        }else {            display = getWindowManager().getDefaultDisplay();            width = display.getWidth();            height = display.getHeight();        }

There is another way, this method can also get the screen size correctly

  new DisplayMetrics();  getWindowManager().getDefaultDisplay().getMetrics(metrics);  width = metrics.widthPixels;  height = metrics.heightPixels;

A simpler way to do it

 width = getResources().getDisplayMetrics().heightPixels; height = getResources().getDisplayMetrics().widthPixels;

The above screen height is included in the status bar and the height of the navigation bar

2. Get Control dimensions

If we call GetWidth (), Getmeasuredwidth () in the OnCreate () method, we get a size of 0, because in OnCreate (), our controls are not yet drawn, and so OnCreate () is done, Our controls are measured, and we can register a listener to listen to the measurement results.

ViewTreeObserver vto  = mButton.getViewTreeObserver();        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {            @Override            publicvoidonGlobalLayout() {            //移除上一次监听,避免重复监听                mButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);                //在这里调用getHeight()获得控件的高度                buttonHeight = mButton.getHeight();            }        });
3. Get the height of the status bar/notification bar
  Public Static int Getstatusbarheight(Context context) {class<?> c =NULL; Object obj =NULL; Field field =NULL;intx =0, Statusbarheight =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 ());        Statusbarheight = Context.getresources (). getdimensionpixelsize (x); }Catch(Exception E1)        {E1.printstacktrace (); }returnStatusbarheight; }
4. Get the height of the navigation bar
publicintgetNavigationBarHeight(Activity activity) {        Resources resources = activity.getResources();        int resourceId = resources.getIdentifier("navigation_bar_height","dimen""android");        //获取NavigationBar的高度        int height = resources.getDimensionPixelSize(resourceId);        return height;    }
5. Remove the navigation bar

In the Oncraete () method, the following code is called before the Setcontentview ();

 requestWindowFeature(Window.FEATURE_NO_TITLE);
6. Remove status bar/notification bar

In the Oncraete () method, the following code is called before the Setcontentview ();

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Get Android screen size, control size, status bar/notification bar height, navigation bar height

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.