StatusBarCompat Documentation

repository·master·Indexed 21 days ago

https://github.com/msdx/status-bar-compat

A compatibility library for Android (API 19+) that enables developers to set system status bar colors and manage status bar icon brightness (light/dark mode) across various manufacturer ROMs, including MIUI, Flyme, and EMUI.

Tokens
1.1K
Snippets
4
Records
6
Agent score
27%

What's inside StatusBarCompat

  1. Compatibility and ROM support for StatusBarCompat

    master

    StatusBarCompat provides compatibility for various Android versions and manufacturer ROMs:

    ROM Support

    • MIUI: Uses Xiaomi's specific APIs.
    • Flyme: Uses Meizu's APIs and includes extra logic to prevent incompatibility.
    • EMUI: For EMUI 5.0 and above, it uses the standard API; for EMUI 3.1 and below, it uses the Android 4.4.2 approach.

    Limitations and Warnings

    • Dark Icons: Only supported on Android 6.0+, MIUI, and Flyme. Other devices below 6.0 cannot set dark icons.
    • Visibility Warning: Setting the status bar to white or near-white colors may make time and icons invisible on some devices (e.g., Lenovo TAB S8-50F, Lenovo A936) because these devices lack the API to switch icon colors to dark. It is recommended to avoid very light colors unless using a supported ROM like MIUI or Flyme.
  2. Fix status bar and action bar spacing issues

    master

    If you notice an unexpected gap between the status bar and the action bar that is equal in size to the status bar, use resetActionBarContainerTopMargin to fix the layout.

    StatusBarCompat.resetActionBarContainerTopMargin(getWindow(), android.support.v7.appcompat.R.id.action_bar_container);
  3. Install StatusBarCompat

    master

    To use StatusBarCompat in your Android project, follow these two steps:

    1. Configure Repository: Ensure jcenter() is declared in your root project's build.gradle file.
    2. Add Dependency: Add the library to your module's build.gradle file using the compile configuration.
    // In root build.gradle
    allprojects {
        repositories {
            jcenter()
        }
    }
    
    // In module build.gradle
    dependencies {
        compile 'com.githang:status-bar-compat:0.7'
    }
  4. Troubleshoot layout issues on older Android versions

    master

    Because systems below Android 6.0 use fitsSystemWindows to implement transparent status bars, you might encounter issues where the UI does not display correctly in areas where bottom virtual navigation bars are hidden.

    Solution: Ensure your App theme is one of the AppCompat themes.

  5. Use StatusBarCompat.setStatusBarColor()

    master

    Call StatusBarCompat.setStatusBarColor() in your Activity after calling setContentView(). This allows you to set the status bar background color and, on supported devices, toggle between light and dark status bar icons.

    Note on Dark Status Bar Icons: Support for dark status bar icons is available on Android 6.0+ and specific ROMs like MIUI and Flyme. On devices below Android 6.0, dark icons cannot be set via official APIs.

    // Set color and specify if the status bar is light (true) or dark (false)
    StatusBarCompat.setStatusBarColor(this, color, lightStatusBar);
    
    // Or set only the color
    StatusBarCompat.setStatusBarColor(this, color);
  6. Set light status bar mode with LightStatusBarCompat

    master

    Use LightStatusBarCompat.setLightStatusBar(Window window, boolean lightStatusBar) to control the appearance of status bar icons.

    When lightStatusBar is set to true, the status bar icons will be dark (optimized for light-colored status bar backgrounds). When false, the icons will be light (optimized for dark-colored status bar backgrounds).

    The library automatically handles compatibility across different Android versions and manufacturer-specific implementations, including:

    • Android M (API 23) and above: Uses standard View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.
    • Xiaomi (MIUI): Uses reflection to access MiuiWindowManager$LayoutParams and EXTRA_FLAG_STATUS_BAR_DARK_MODE.
    • Meizu (Flyme): Uses reflection to access MEIZU_FLAG_DARK_STATUS_BAR_ICON via WindowManager.LayoutParams.
    // To set dark icons (for a light status bar background)
    LightStatusBarCompat.setLightStatusBar(getWindow(), true);
    
    // To set light icons (for a dark status bar background)
    LightStatusBarCompat.setLightStatusBar(getWindow(), false);