Android Transparent status bar. Suitable for over 4.4 and over 5.0 devices

Source: Internet
Author: User

Overview

Sometimes we want to implement an effect across the top status bar on a andorid phone, such as a picture that appears directly within the status bar. Like what:

There is a picture on this page that is displayed in the upper part of the page. The status bar is floating on this picture.

Ways to implement transparent status bars

Ways to fit Android 4.4 +:

if (Build.VERSION.SDK_INT >= build.version_codes. KITKAT) {            = Activity.getwindow ();             // Translucent status Bar             window.setflags (                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);             // Translucent navigation bar             window.setflags (                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);        }

Ways to fit your Android 5.0+:

if (Build.VERSION.SDK_INT >= build.version_codes. LOLLIPOP) {            = Activity.getwindow ();            Window.clearflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS                    |  WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);            Window.getdecorview (). setsystemuivisibility (view.system_ui_flag_layout_fullscreen                    |  View.system_ui_flag_layout_hide_navigation                    | view.system_ui_flag_layout_stable);            Window.addflags (WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);            Window.setstatusbarcolor (color.transparent);            Window.setnavigationbarcolor (color.transparent);        }

How to use

It is called before the Setcontentview method in the OnCreate method of the activity. Like what:

    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {         Super . OnCreate (savedinstancestate);        GetWindow (). Requestfeature (Window.feature_no_title);
Call Statusbarutils.enabletranslucentstatusbar here (this
); Setcontentview (R.layout.activity_main); }

In the code above, I use Statusbarutils, the helper class, which invokes a concrete method for implementing transparent status bars. The complete code for this class is as follows:

 PackageZhangyf.vir56k.translucentbardemo;Importandroid.app.Activity;ImportAndroid.graphics.Color;ImportAndroid.os.Build;ImportAndroid.view.View;ImportAndroid.view.Window;ImportAndroid.view.WindowManager;/*** status bar Auxiliary class * Created by Zhangyunfei on 16/6/17.*/ Public classStatusbarutils {/*** Enable transparent status bar *@paramActivity*/     Public Static voidEnabletranslucentstatusbar (activity activity) {if(Build.VERSION.SDK_INT >=Build.version_codes. LOLLIPOP) {window window=Activity.getwindow (); Window.clearflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS|WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); Window.getdecorview (). setsystemuivisibility (View.system_ui_flag_layout_fullscreen|view.system_ui_flag_layout_hide_navigation|view.system_ui_flag_layout_stable);            Window.addflags (WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);            Window.setstatusbarcolor (color.transparent);        Window.setnavigationbarcolor (color.transparent); }        if(Build.VERSION.SDK_INT >=Build.version_codes. KITKAT) {window window=Activity.getwindow (); //Translucent status BarWindow.setflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //Translucent navigation barWindow.setflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,        WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); }    }}

Android:fitssystemwindows Label

Android provides a android:fitssystemwindows tag to fit the form, and we can actually use it in our layout view. It causes the specified view to automatically increase the "top gap in the height of a status bar", that is, it will ensure that your view is below the status bar, not underneath.

Android:fitssystemwindows= "true"

Sample source code Download
Https://github.com/vir56k/demo/tree/master/TranslucentBarDemo

Android Transparent status bar. Suitable for over 4.4 and over 5.0 devices

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.