Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:fitsSystemWindows="true" has issues with using DrawerLayout and immersive mode

I'm having issues when trying to use immersive mode and using android:fitsSystemWindows="true" with DrawerLayout. I have to set this to true for the DrawerLayout and toolbar to be constrained to the system bar.

The issue is, I have a Fragment which sets the application to immersive mode. This makes the app do this: Fullscreen with system bars

Which I know is a known issue with android:fitsSystemWindows="true". I set it to false and immersive mode works well but the toolbar and the rest of the layout is no longer constrained to the system bars. I try setting the value at runtime and the lower portion of the layout (aka the navigation bar area) is filled by my layout, but the system bar area is still showing: Fullscreen with status bar

Here is my code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

ClockFragment.java - Hide and Show

private void hideSystemUI() {
    if (Build.VERSION.SDK_INT >= 14) {
        getActivity().findViewById(R.id.drawer_layout).setFitsSystemWindows(false);
    }
    getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    if (Build.VERSION.SDK_INT >= 19) {
        mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
    } else if (Build.VERSION.SDK_INT >= 16) {
        mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
    } else {
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    toolbar.setVisibility(View.GONE);
    layoutParams.screenBrightness = -1.00f;
    getActivity().getWindow().setAttributes(layoutParams);
    drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    uiShowing = false;
    dimming = false;
    screenMode = 1;
}

private void hideSystemUIAndDim() {
    if (Build.VERSION.SDK_INT >= 14) {
        getActivity().findViewById(R.id.drawer_layout).setFitsSystemWindows(false);
    }
    getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    if (Build.VERSION.SDK_INT >= 19) {
        mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
    } else if (Build.VERSION.SDK_INT >= 16) {
        mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
    } else {
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    toolbar.setVisibility(View.GONE);
    layoutParams.screenBrightness = 0.01f;
    getActivity().getWindow().setAttributes(layoutParams);
    drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    uiShowing = false;
    dimming = true;
    screenMode = 2;
}

private void showSystemUI() {
    if (Build.VERSION.SDK_INT >= 14) {
        getActivity().findViewById(R.id.drawer_layout).setFitsSystemWindows(true);
    }
    if (Build.VERSION.SDK_INT >= 16) {
        mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    } else {
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    }
    toolbar.setVisibility(View.VISIBLE);
    layoutParams.screenBrightness = -1.00f;
    getActivity().getWindow().setAttributes(layoutParams);
    drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
    uiShowing = true;
    dimming = false;
    screenMode = 0;
}
like image 614
Timmo Avatar asked Mar 25 '16 19:03

Timmo


1 Answers

OK I finally fixed my problem. Turns out that I needed to add the FLAG_FULLSCREEN flag. Here is my fixed code for future viewers:

private void hideSystemUI() {
    if (Build.VERSION.SDK_INT >= 14) {
        drawerLayout.setFitsSystemWindows(false);
    }
    getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    if (Build.VERSION.SDK_INT >= 19) {
        mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
    } else if (Build.VERSION.SDK_INT >= 16) {
        mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
    }
    toolbar.setVisibility(View.GONE);
    layoutParams.screenBrightness = -1.00f;
    getActivity().getWindow().setAttributes(layoutParams);
    drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    uiShowing = false;
    dimming = false;
    screenMode = 1;
}

private void hideSystemUIAndDim() {
    if (Build.VERSION.SDK_INT >= 14) {
        drawerLayout.setFitsSystemWindows(false);
    }
    getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    if (Build.VERSION.SDK_INT >= 19) {
        mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE);
    } else if (Build.VERSION.SDK_INT >= 16) {
        mDecorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
    }
    toolbar.setVisibility(View.GONE);
    layoutParams.screenBrightness = 0.01f;
    getActivity().getWindow().setAttributes(layoutParams);
    drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    uiShowing = false;
    dimming = true;
    screenMode = 2;
}

private void showSystemUI() {
    try {
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        if (Build.VERSION.SDK_INT >= 14) {
            drawerLayout.setFitsSystemWindows(true);
        }
        if (Build.VERSION.SDK_INT >= 16) {
            mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
        toolbar.setVisibility(View.VISIBLE);
        layoutParams.screenBrightness = -1.00f;
        getActivity().getWindow().setAttributes(layoutParams);
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        uiShowing = true;
        dimming = false;
        screenMode = 0;
    } catch (NullPointerException e) {
        Log.e(TAG, e.toString());
    }
}
like image 81
Timmo Avatar answered Sep 22 '22 02:09

Timmo