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 screen size through the GetSize () method

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

Suppose it's not in the activity. You cannot use Getwindowmanager (). At this point, 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. We need to do this before API 13.

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

In order to fit all the equipment. We should write that.

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 a second way, and 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 the height of the status bar and the navigation bar.

2. Get Control dimensions

Suppose we get a size of 0 in the OnCreate () method directly called GetWidth (), Getmeasuredwidth (). This is because in OnCreate (), our controls have not been painted well. When OnCreate () runs out, our controls are measured, and we are able to register a listener to monitor the measured 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

The Setcontentview () in the Oncraete () method. Before calling the following code

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

Get Android screen size, control size, status bar/notification bar height, navigation bar 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.