Immersive status bar for Android

Source: Internet
Author: User

Immersive status bar for Android

In the previous article, we implemented the color-changing status bar for Android, and introduced the differences between the immersive Status Bar and the transparent status bar. In this article, we implemented the immersive status bar.

The source of the immersive status bar is that many mobile phones use solid buttons without virtual keys. When the immersive mode is enabled, the status bar disappears. So the immersion mode becomes the immersive status bar.

Let's take a look at the specific effect.

After the immersion mode is enabled, the status bar disappears and slides down from the top. The status bar appears and exits the immersion mode. The status bar also appears. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vcd4ncjxwps7sw8e1xlt6wu7 + dPax7DSu8aqzsTVwqGjytfPyMrHwb249r + qxvSzwb3 + placement = "brush: java;"> @SuppressLint(NewApi)public static void hideSystemUI(View view) { view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);}@SuppressLint(NewApi)public static void showSystemUI(View view) { view.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);}

These codes can be found in google's developer documentation. You can refer to Using Immersive Full-Screen Mode. The above Code takes effect only in Android 4.4, determine the compatibility of the corresponding Android version.

In addition, an auxiliary function is required to obtain the height of the status bar, which is obtained through reflection.

/*** Get the status bar height ** @ param context Context * @ return notice Bar Height */public int getStatusBarHeight (context) {int statusBarHeight = 0; try {Class
  Clazz = Class. forName (com. android. internal. r$ dimen); Object obj = clazz. newInstance (); Field field = clazz. getField (status_bar_height); int temp = Integer. parseInt (field. get (obj ). toString (); statusBarHeight = context. getResources (). getDimensionPixelSize (temp);} catch (Exception e) {e. printStackTrace ();} return statusBarHeight ;}

Click the hide button to enter the immersion mode, that is, to hide the status bar. while hiding the status bar, we need to modify the top and bottom margins of the Toolbar. Otherwise, it will become ugly. Here we register a listener OnSystemUiVisibilityChangeListener, when we enter the immersion mode, we change the top margin of the Toolbar.

hide.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        View view = getWindow().getDecorView();        hideSystemUI(view);        mToolbar.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {            @Override            public void onSystemUiVisibilityChange(int visibility) {                mToolbar.setPadding(mToolbar.getPaddingLeft(), 0,mToolbar.getPaddingRight(), mToolbar.getPaddingBottom());            }        });    }});

After entering the immersive mode, move your finger down from the top of the screen, and the status bar appears. It will automatically disappear in about 2 seconds.

Click show to exit the immersion mode. The padding of the Toolbar must also be increased to the height of the status bar.

show.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        View view = getWindow().getDecorView();        showSystemUI(view);        mToolbar.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {            @Override            public void onSystemUiVisibilityChange(int visibility) {                mToolbar.setPadding(mToolbar.getPaddingLeft(), getStatusBarHeight(MainActivity.this),mToolbar.getPaddingRight(), mToolbar.getPaddingBottom());            }        });    }});

For more information, see the preceding section.

If you use the SystemBarTintManager class to color the status bar, in addition to the above operations, you also need to add the function of disabling and starting the status bar coloring in the corresponding listener.

Enter immersion mode. Disable

tintManager.setStatusBarTintEnabled(false);

Exit immersion mode. Start

tintManager.setStatusBarTintEnabled(true);

If you want to make the animation smoother, you can add an animation for the padding.

Remember to judge the Android version before using the immersive mode. SYSTEM_UI_FLAG_IMMERSIVE_STICKY can be used only when the value is greater than or equal to API Level 19. You must be compatible with earlier versions and use the immersive mode. Before using SYSTEM_UI_FLAG_IMMERSIVE_STICKY, use android. OS. Build. VERSION. SDK_INT to determine whether the current system VERSION is android4.4 or above. If yes, enable the Code. If not, skip and do not execute.

 

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.