Obtain the Android screen size, widget size, status bar/notification bar height, navigation bar height, and android screen size.

Source: Internet
Author: User

Obtain the Android screen size, widget size, status bar/notification bar height, navigation bar height, and android screen size.
1. Get Android screen size

We can use the getSize () method to obtain the screen size.

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

If it is not in the Activity, getWindowManager () cannot be used. In this case, you can use WINDOW_SERVICE to obtain a default Display.

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

The getSize () method is added after API 13. We need to do this before API 13.

Display the Display = getWindowManager (.) getDefaultDisplay ();
Int width = display. GetWidth (); // it is out of date
Int height = display. GetHeight (); // it is out of date

To adapt to all devices, we should write

if (android.os.Build.VERSION.SDK_INT >= 13) {
            display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            width = size.x;
            height = size.y;
        }else {
            display = getWindowManager().getDefaultDisplay();
            width = display.getWidth();
            height = display.getHeight();
        }

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

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

Easier writing

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

The screen height obtained above includes the height of the status bar and navigation bar.

2. Get the widget size

If we call getWidth () and getMeasuredWidth () in the onCreate () method to obtain a size of 0, this is because our control has not been properly painted in onCreate, after onCreate () is executed, our control is measured. We can register a listener to listen on the measurement results.

ViewTreeObserver vto = mButton. GetViewTreeObserver ();
Vto. AddOnGlobalLayoutListener (new OnGlobalLayoutListener () {

@ Override
Public void onGlobalLayout () {
// remove last monitor to avoid duplicate monitor
MButton. GetViewTreeObserver (.) removeGlobalOnLayoutListener (this);
// getHeight () is called here to get the height of the control
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; int x = 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();
        } return statusBarHeight;
    }
4. Obtain the height of the navigation bar
 
Public int getNavigationBarHeight(Activity Activity) {
Resources Resources = activity. GetResources ();
Int resourceId = resources.getidentifier ("navigation_bar_height","dimen", "android");
// gets the height of the NavigationBar
Int height = resources. GetDimensionPixelSize (resourceId);
Return the height;
}
5. Remove the navigation bar

Call the following code before setContentView (); In the onCraete () method

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

Call the following code before setContentView (); In the onCraete () method

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

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.


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.