Skip to content

Instantly share code, notes, and snippets.

@bdiegel
Created October 28, 2015 14:37
Show Gist options
  • Save bdiegel/e50cdb9f1928c28a2abb to your computer and use it in GitHub Desktop.
Save bdiegel/e50cdb9f1928c28a2abb to your computer and use it in GitHub Desktop.
Android Transparent Status Bar Fix

Fix status bar transparency

Note the adjusting the insets is a specific workaround for views that do not respect layout settings. View Pager has such problems.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setStatusBarTransparent() {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().getDecorView().setSystemUiVisibility(
          View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    getWindow().setStatusBarColor(Color.TRANSPARENT);
    
    // adjust insets to fix status bar transparency issue
    ViewGroup v = (ViewGroup) findViewById(R.id.root);
    v.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
            return insets.consumeSystemWindowInsets();
        }
    });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment