Effective acquisition of the status bar (StatusBar) height for Android Development

Source: Internet
Author: User

Retrieve the Status Bar Height


1. Traditional Method: Sometimes the value is 0. solution 2:

 
Code
Rect frame = new Rect ();
GetWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame );
Int statusBarHeight = frame. top;
 

II. The value may be 0 after 4.0.3.

Public int getBarHeight (){
Class <?> C = null;
Object obj = null;
Field field = null;
Int x = 0, sbar = 38; // The default value is 38. It seems that most of them are like this.

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 ());
Sbar = getResources (). getDimensionPixelSize (x );

} Catch (Exception e1 ){
E1.printStackTrace ();
}
Return sbar;
}

 

//---------------------------------------------

 


1. Get the status bar height:

DecorView is the top-level view in the window. You can obtain the decorView from the window. Then, decorView has a getWindowVisibleDisplayFrame method to obtain the display area of the program, including the title bar, but not the status bar.

So we can calculate the height of the status bar.

 
Code
Rect frame = new Rect ();
GetWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame );
Int statusBarHeight = frame. top;
 

--------------------------------------------------------------------------------

Sometimes the obtained height is 0, which can be obtained in another way.

Obtain the status bar height code in the source code program:

Height = getResources (). getDimensionPixelSize (com. android. internal. R. dimen. status_bar_height );

 
6. Code
Class c = Class. forName ("com. android. internal. R$ dimen ");
Object obj = c. newInstance ();
Field field = c. getField ("status_bar_height ");
Int x = Integer. parseInt (field. get (obj). toString ());
Int y = getResources (). getDimensionPixelSize (x );
 

2. Obtain the title bar height:

The view obtained by the getWindow (). findViewById (Window. ID_ANDROID_CONTENT) method is the part of the title bar that the program does not contain. Then you can know the height of the title bar.

 
Code
Int contentTop = getWindow (). findViewById (Window. ID_ANDROID_CONTENT). getTop ();
// StatusBarHeight is the height of the status bar requested above
Int titleBarHeight = contentTop-statusBarHeight

 

3. Get screen height

Method 1.

Java code

 
Code
WindowManager windowManager = getWindowManager ();
Display display = windowManager. getDefaultDisplay ();
ScreenWidth = display. getWidth ();
ScreenHeight = display. getHeight ();
 

Method 2.

 
Java code
DisplayMetrics dm = new DisplayMetrics ();
This. getWindowManager (). getDefaultDisplay (). getMetrics (dm); // this indicates the current activity
ScreenWidth = dm. widthPixels;
ScreenHeight = dm. heightPixels;
 

The above two methods are still in the 0 state when the screen is not displayed, that is, they are valid only after setContentView is called.

Set to untitled

 

RequestWindowFeature (Window. FEATURE_NO_TITLE );

Set to full screen mode getWindow (). setFlags

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

Set to landscape

SetRequesteOrientation (ActivityInfo. SCREEN_ORIENTATION_LADSCAPE );

 

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.