Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Immersive Status Bar in Android Applications

Tech May 15 1

Status Bar Customization Approaches

Android provides different methods for status bar customizatino across versions:

  • Android 4.4-5.0: Requires creating a transparent status bar with a colored view overlay
  • Android 5.0-6.0: Introduces setStatusBarColor() but lacks native text color control
  • Android 6.0+: Supports both status bar color and text/icon color modification

Implementation Details

Basic Setup

First, modify the app theme to remove the default action bar:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

Create a utility class to handle status bar operations:

public class StatusBarHelper {
    public static int getStatusBarHeight(Context ctx) {
        int height = 0;
        try {
            int resId = ctx.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resId > 0) height = ctx.getResources().getDimensionPixelSize(resId);
        } catch (Exception e) { /* Handle exception */ }
        return height;
    }
}

Android 5.0+ Implementation

public static void applyStatusBarColor(Window window, int color) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        window.setStatusBarColor(color);
        adjustTextColor(window, color);
    }
}

private static void adjustTextColor(Window window, int backgroundColor) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        View decor = window.getDecorView();
        int flags = decor.getSystemUiVisibility();
        boolean darkText = ColorUtils.calculateLuminance(backgroundColor) > 0.5;
        decor.setSystemUiVisibility(darkText ? 
            flags | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR : 
            flags & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }
}

Android 4.4 Implmeentation

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static void applyLegacyStatusBar(Window window, int color) {
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    ViewGroup decor = (ViewGroup) window.getDecorView();
    View content = decor.findViewById(android.R.id.content);
    if (content != null) content.setPadding(0, getStatusBarHeight(window.getContext()), 0, 0);
    
    View statusBarView = new View(window.getContext());
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, 
        getStatusBarHeight(window.getContext()));
    statusBarView.setLayoutParams(params);
    statusBarView.setBackgroundColor(ColorUtils.blendARGB(Color.BLACK, color, 0.5f));
    decor.addView(statusBarView);
}

Notch Display Adaptation

For full-screen activities like splash screens:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.layoutInDisplayCutoutMode = 
        WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
    getWindow().setAttributes(params);
}

Add manufacturer-specific adaptations in AndroidManifest.xml:

<meta-data android:name="notch.config" android:value="portrait" />
<meta-data android:name="android.notch_support" android:value="true" />
<meta-data android:name="android.max_aspect" android:value="2.2" />

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.