Realization of Android Studio Full Screen Immersive Transparent Status Bar Effect

  • 2021-12-09 10:15:19
  • OfStack

How to achieve it? 1.) Full screen first

Type 1: Inherit topic-specific topic

Above Android API 19, you can use ****.TranslucentDecor*** related themes with corresponding translucent effect. Theme. Holo. NoActionBar. TranslucentDecor and Theme. Holo. Light. NoActionBar. TranslucentDecor are newly added, so you should create a new values-v19 folder and create styles file and add the following code


<style name="AppBaseTheme" parent="android:Theme.Holo.Light.NoActionBar.TranslucentDecor">
  <!-- Customize your theme here. -->
</style>

Type 2: Using code in activity

The following code can be added above Android 4.4


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Transparent status bar 
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// Transparent navigation bar 
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}

Android 5.0 and above can also use the following code to achieve full screen


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
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);
}

2.) Solve the status bar occupancy problem

Type 1: Add the following settings to the theme


<item name="android:fitsSystemWindows">true</item>

Type 2: activity layout Root Add the following code


android:fitsSystemWindows="true"

Type 3: Setting through Java code


rootview.setFitsSystemWindows(true);

3.) Setting the background color for the status bar navigation bar

For those above 4.4, you can modify the background color of contentView or dynamically add 1 view to contentView


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      // Transparent status bar 
      window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      // Transparent navigation bar 
      window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
      // Settings contentview For fitsSystemWindows
      ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);
      View childAt = contentView.getChildAt(0);
      if (childAt != null) {
          childAt.setFitsSystemWindows(true);
      }
      // To statusbar Coloring 
      View view = new View(this);
      view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(this)));
      view.setBackgroundColor(color);
      contentView.addView(view);
}

The dynamic fetch StatusBarHeight function is as follows


/**
  *  Get the height of the status bar 
  *
  * @param context context
  * @return  Height of status bar 
  */
 private static int getStatusBarHeight(Context context) {
  //  Get the height of the status bar 
  int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
  return context.getResources().getDimensionPixelSize(resourceId);
 }

The dynamic acquisition NavigationBarHeight function is as follows


/**
  *  Get navigation bar height 
  *
  * @param context context
  * @return  Navigation bar height 
  */
 public static int getNavigationBarHeight(Context context) {
  int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
  return context.getResources().getDimensionPixelSize(resourceId);
 }

4.) Post the overall java code implementation


private void initWindows() {
  Window window = getWindow();
  int color = getResources().getColor(R.color.wechatBgColor);
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   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);
   // Set the status bar color 
   window.setStatusBarColor(color);
   // Set the navigation bar color 
   window.setNavigationBarColor(getResources().getColor(R.color.footerBgColor));
   ViewGroup contentView = ((ViewGroup) findViewById(android.R.id.content));
   View childAt = contentView.getChildAt(0);
   if (childAt != null) {
    childAt.setFitsSystemWindows(true);
   }
  } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
   // Transparent status bar 
   window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
   // Transparent navigation bar 
   window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
   // Settings contentview For fitsSystemWindows
   ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);
   View childAt = contentView.getChildAt(0);
   if (childAt != null) {
    childAt.setFitsSystemWindows(true);
   }
   // To statusbar Coloring 
   View view = new View(this);
   view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(this)));
   view.setBackgroundColor(color);
   contentView.addView(view);
  }
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && useStatusBarColor) {//android6.0 You can modify the text color and icon of the status bar later 
   getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
  }
 }


Related articles: