Skip to content

Instantly share code, notes, and snippets.

@Tanapruk
Last active July 25, 2024 16:05
Show Gist options
  • Save Tanapruk/abe9d5f6a655b0e71b03579656fda64d to your computer and use it in GitHub Desktop.
Save Tanapruk/abe9d5f6a655b0e71b03579656fda64d to your computer and use it in GitHub Desktop.
Status Bar and Navigation bar

Complete Transparent Status Bar & Navigation Bar

In your theme

<item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent</item>
<item name="android:navigationBarColor" tools:targetApi="lollipop">@android:color/transparent</item>

In your layout

android:fitsSystemWindows="true"

In your activity

if (Build.VERSION.SDK_INT >= 21) {
    window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
    toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
    navbar.setPadding(0, getStatusBarHeight(), 0, 0)
}

fun getStatusBarHeight(): Int {
    val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId)
    } else {
        return 0
    }
}

Translucent Status Bar & Navigation Bar

In your theme

<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
<item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item>

In your layout

android:fitsSystemWindows="true"

In your activity

if (Build.VERSION.SDK_INT >= 21) {
    toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
    navbar.setPadding(0, getStatusBarHeight(), 0, 0)
}

fun getStatusBarHeight(): Int {
    val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId)
    } else {
        return 0
    }
}

Colored Status Bar & Navigation Bar

In your theme

<item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/holo_red_dark</item>
<item name="android:navigationBarColor" tools:targetApi="lollipop">@android:color/holo_red_dark</item>
<item name="android:windowDrawsSystemBarBackgrounds" tools:targetApi="lollipop">true</item>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment