I want to have a transparent status bar on my app (so the background goes behind it) but I want the navigation bar at the bottom to stay black.
I can make both transparent by setting getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
I can make the top translucent (partially transparent) by setting <item name="android:windowTranslucentStatus">true</item>
However, I can't make the top completely transparent without making the bottom transparent too. Using <item name="android:statusBarColor">@android:color/transparent</item>
or similar doesn't work.
Does anyone know how to make a fully transparent status bar without affecting the navigation bar?
Creating a transparent navbar is very easy - just don't add a color class . bg-* to the navbar. In this case, the Navbar will take the color of the parent's background color.
To independently control the transluscency of the status and navigation bars on KitKat, you can simply use the window manager flags FLAG_TRANSLUSCENT_STATUS
and FLAG_TRANSLUSCENT_NAVIGATION
in the onCreate()
method of your activity. However, on KitKat the system may draw a semi-opaque gradient scrim drawable over the status bar. This appears to be device-specific: on my KitKat device it's fully transparent, but on Android Studio emulator it shows a scrim.
On Lolliop or later you can instead set the status bar colour using the Window#setStatusBarColor(int)
method together with the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
window manager flag. Note that you clear the FLAG_TRANSLUSCENT_STATUS
flag in this case. If you want the colour to be transparent, that implies that your application supports full screen mode and sets the system UI visibility, so it's down to your app to manage the status bar background colour.
Putting it all together would look something like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(0x00000000); // transparent
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
window.addFlags(flags);
}
setContentView(R.layout.main);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
Example of layout used:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff00"
android:fitsSystemWindows="false"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</LinearLayout>
</RelativeLayout>
Example screenshots.
Lollipop:
KitKat:
Starting from API level 30, most of window flags are deprecated,
Using WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
Will make the activity overlaps with the navigation bar, also makes it transparent.
So, instead to make this work on API level 30, you can use setDecorFitsSystemWindows()
:
if (Build.VERSION.SDK_INT in 21..29) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.decorView.systemUiVisibility =
SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or SYSTEM_UI_FLAG_LAYOUT_STABLE
window.statusBarColor = Color.TRANSPARENT
}
if (Build.VERSION.SDK_INT >= 30) {
// Setting status bar color as not working with XML attribute
window.statusBarColor = Color.TRANSPARENT
// Making status bar overlaps with the activity
WindowCompat.setDecorFitsSystemWindows(window, false)
}
And keep the below in the styles.xml:
<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
UPDATE
If you faced that the bottom part of layout obscured by the bottom navigation; please check this answer fore more in detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With