Immersive status bar for Android

Source: Internet
Author: User

The previous article on the Android implementation of the color status bar we realized the color of the status bar, also introduced the Immersion status bar and the difference between the transparent status bar, this article we implement the immersive status bar.

The source of the immersive status bar is that many mobile phones use the entity keys, there is no virtual key, so open immersion mode only the status bar disappears. Immersive mode becomes an immersive status bar.

Let's take a look at the concrete effects.

When the immersion mode is turned on, the status bar disappears, swipe down from the top, the status bar appears, exits the immersion mode, and the status bar appears.

Our code is based on the previous article. First, there are two functions that turn on immersion mode and turn off immersion mode

@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);}

This code can be found in Google's developer documentation, and can be seen here using Immersive Full-screen Mode, the above code is in Android 4.4 will not take effect, the corresponding version of the Android compatibility of the judgment please handle it yourself.

You also need an auxiliary function to get the height of the status bar, using reflection.

/** * Receive status bar height * * @param Context context * @return notification Bar height */ Public  int Getstatusbarheight(Context context) {intStatusbarheight =0;Try{class<?> clazz = Class.forName ("Com.android.internal.r$dimen");        Object obj = clazz.newinstance (); Field field = Clazz.getfield ("Status_bar_height");inttemp = Integer.parseint (Field.get (obj). toString ());    Statusbarheight = Context.getresources (). Getdimensionpixelsize (temp); }Catch(Exception e)    {E.printstacktrace (); }returnStatusbarheight;}

Click the Hide button to enter the immersion mode, that is, hide the status bar, hide the status bar at the same time we need to modify the toolbar on the inside margin, otherwise it will appear very ugly, here register a monitoring Onsystemuivisibilitychangelistener, We change the top padding of the toolbar when we enter immersion mode

hide.setonclicklistener (new  View.onclicklistener () { @Override  public< /span> void  onclick  (view v) {View view = Getwind        ow (). Getdecorview ();        Hidesystemui (view); Mtoolbar.setonsystemuivisibilitychangelistener (new  View.onsystemuivisibilitychangelistener () { @Override  public  void   Onsystemuivisibilitychange  (int  visibility) {mtoolbar.setpadding (M Toolbar.getpaddingleft (), 0 , Mtoolbar.getpaddingright (),            Mtoolbar.getpaddingbottom ());    }        }); }});

After entering immersion mode, the finger is drawn down from the top of the screen, the status bar appears, and it will automatically disappear after about 2 seconds.

Click the Show button to exit the immersion mode, and the toolbar padding will increase to the height of the status bar.

show.setonclicklistener (new  View.onclicklistener () { Span class= "hljs-annotation" > @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 (M Toolbar.getpaddingleft (), Getstatusbarheight (Mainactivity. This ), Mtoolbar.getpaddingright (), Mtoolbar.getpaddingbottom ()); } }); }});

See above for concrete effects.

If you use the Systembartintmanager this class to do the coloring of the status bar, in addition to the above operation, but also in the corresponding monitoring to increase the status bar coloring the function of the prohibition and start.

Enter immersion mode, to disable

tintManager.setStatusBarTintEnabled(false);

Exit immersion mode, to start

tintManager.setStatusBarTintEnabled(true);

If you want to be smoother, you can add animations to the padding and animate them yourself.

Remember to determine the version of Android before you use immersion mode. System_ui_flag_immersive_sticky can only be used at a level greater than or equal to API level 19. You want to be compatible with the lower version while using immersive mode. Use android.os.Build.VERSION.SDK_ before using System_ui_flag_immersive_sticky int to determine if the current system version is more than android4.4, if it is on, the code is enabled, and if not, the skip is not performed.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Immersive status bar for Android

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.