How Android gets system height, title bar, and status bar height

Source: Internet
Author: User

In Android applications, it is sometimes necessary to calculate the location of a view, resulting in the need to calculate status bar height, title bar height and other information. For the convenience of the future, make a simple record here.

Before drying the code, understand the division of the Android screen area, such as (the figure is referenced from this article http://www.iteye.com/topic/828830)

1 , capture of the screen area

[Java]View Plaincopy
    1. Activity.getwindowmanager (). Getdefaultdisplay ();

2 , access to the application area

[Java]View Plaincopy
    1. Rect outrect = new Rect ();
    2. Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (Outrect);

Where Outrect.top is the height of the status bar.

3. View Drawing Area Acquisition

[Java]View Plaincopy
    1. Rect outrect = new Rect ();
    2. Activity.getwindow (). Findviewbyid (window.id_android_content). Getdrawingrect (Outrect);

With the outrect.top of the drawing area-the outrect.top of the applied area is the height of the title bar.

Note: If you are just starting the activity to calculate this data, it is best to do in the onwindowfocuschanged function, otherwise the resulting data may be wrong, for example, the application area of high-wide acquisition.

The detailed code is as follows:

[Java]View Plaincopy
  1. Public class ScreenSize extends Activity {
  2. private TextView Mscreensizeview;
  3. @Override
  4. public void OnCreate (Bundle savedinstancestate) {
  5. super.oncreate (savedinstancestate);
  6. Setcontentview (r.layout.activity_screen_size);
  7. Mscreensizeview = (TextView) Findviewbyid (r.id.screen_size);
  8. }
  9. @Override
  10. public void Onwindowfocuschanged (boolean hasfocus) {
  11. super.onwindowfocuschanged (Hasfocus);
  12. if (hasfocus) {
  13. System.out.println ("second");
  14. StringBuilder sb = new StringBuilder ();
  15. Dimension dimen1 = Getareaone (this);
  16. Dimension Dimen2 = Getareatwo (this);
  17. Dimension Dimen3 = Getareathree (this);
  18. Sb.append ("area one: \n\twidth:" +dimen1.mwidth + "; \theight:" +dimen1.mheight);
  19. Sb.append ("\narea: \n\twidth:" +dimen2.mwidth + "; \theight:" +dimen2.mheight);
  20. Sb.append ("\narea Three: \n\twidth:" +dimen3.mwidth + "; \theight:" +dimen3.mheight);
  21. Mscreensizeview.settext (Sb.tostring ());
  22. }
  23. }
  24. @Override
  25. Public Boolean oncreateoptionsmenu (Menu menu) {
  26. Getmenuinflater (). Inflate (r.menu.activity_screen_size, menu);
  27. return true;
  28. }
  29. Private Dimension Getareaone (activity activity) {
  30. Dimension dimen = new Dimension ();
  31. Display disp = Activity.getwindowmanager (). Getdefaultdisplay ();
  32. Point outp = new Point ();
  33. Disp.getsize (OUTP);
  34. Dimen.mwidth = outp.x;
  35. Dimen.mheight = OUTP.Y;
  36. return dimen;
  37. }
  38. Private Dimension getareatwo (activity activity) {
  39. Dimension dimen = new Dimension ();
  40. Rect outrect = new Rect ();
  41. Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (Outrect);
  42. System.out.println ("Top:" +outrect.top +";  Left: "+outrect.left");
  43. Dimen.mwidth = Outrect.width ();
  44. Dimen.mheight = Outrect.height ();
  45. return dimen;
  46. }
  47. Private Dimension Getareathree (activity activity) {
  48. Dimension dimen = new Dimension ();
  49. //user Draw area
  50. Rect outrect = new Rect ();
  51. Activity.getwindow (). Findviewbyid (window.id_android_content). Getdrawingrect (Outrect);
  52. Dimen.mwidth = Outrect.width ();
  53. Dimen.mheight = Outrect.height ();
  54. //End
  55. return dimen;
  56. }
[Java]View Plaincopy
    1. Private class Dimension {
    2. public int mwidth;
    3. public int mheight;
    4. Public Dimension () {}
    5. }

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.