Get the height of the android Status Bar

Source: Internet
Author: User

In Android development, you often need to know the screen height, width, Status Bar, and Title Bar Height.

Width and height

WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);Display display = windowManager.getDefaultDisplay();Point point = new Point();display.getSize(point);SCREEN_WIDTH = point.x;SCREEN_HEIGHT = point.y;System.out.println("SCREEN_WIDTH = " + SCREEN_WIDTH);System.out.println("SCREEN_HEIGHT = " + SCREEN_HEIGHT);

Status Bar Height

If you are working on Rom, you can use the following methods:

int id = com.android.internal.R.dimen.status_bar_height;STATUS_BAR_HEIGHT = (int) getResources().getDimension(id);

The above method only applies to Rom, because the id value has been assigned during compilation, such as COM. android. internal. r. dimen. status_bar_height = 123; the actual code is like this.

int id = 123;STATUS_BAR_HEIGHT = (int) getResources().getDimension(id);

Another method is the improvement of the above method. The Java reflection principle is used to obtain the height.

int id = 0;try {Class<?> cls = Class.forName("com.android.internal.R$dimen");Field field = cls.getField("status_bar_height");id = field.getInt(cls);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (NoSuchFieldException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace);}

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.