Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translucent navigation without layout displaying beneath status bar

I am using the new Android 4.4 FLAG_TRANSLUCENT_NAVIGATION flag to turn the navigation bar (back, home button etc) at the bottom of the screen translucent. This works fine but a side effect of this is the layout of my Activity now displays beneath the status bar at the top of the screen (even though I have not set the status bar as being translucent). I want to avoid a hacky fix I.e. applying padding to the top of the layout.

Is there a way to set the navigation as translucent whilst ensuring the status bar appears normally and does NOT allow the layout to display beneath it?

The code I am using is as follows:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    Window w = getWindow();
    w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}

Thanks

like image 543
Milo Avatar asked Mar 22 '23 08:03

Milo


1 Answers

ianhanniballake's answer actually works, but you shouldn't use android:fitsSystemWindows="true" on the toplevel view. Use this property as following :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorPrimaryDark">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#ffffff"
        android:fitsSystemWindows="true">

        <!-- Your other views -->

    </LinearLayout>
</LinearLayout>

As you can see, you have to set the color on the top-level view, and put the property on another container.

like image 157
Quentin S. Avatar answered Apr 06 '23 15:04

Quentin S.