Instead of trying to figure out whether you are in multi-window mode and on which part of the screen, you need to make your viewing title in accordance with the system window inserts.
Usually you only care about one window - the volume in which your application is embedded. Usually you don’t even think that there are any windows. Isn't your application drawn in full screen? Well, actually not. Usually there is some space for system bars, for example, a status bar at the top and a navigation bar at the bottom. They are drawn in separate windows - system windows. (Oh, and now we have multi-window mode in N. More like multi-window mode, because if you count system windows, then multi-window exists for some time.)
You can customize the title of your navigation view, adjust its inserts depending on whether it is under the system window (in this case: the status bar) or not with just a few simple settings.
Assuming that the type of navigation is defined as follows:
<android.support.design.widget.NavigationView ... android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" ... />
and nav_header_main.xml has a simple header:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height" android:background="@drawable/nav_header_background" android:orientation="vertical" android:paddingBottom="16dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="32dp"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/default_profile_picture" /> ... </LinearLayout>
you just need to change it like this:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height" android:background="@drawable/nav_header_background" android:fitsSystemWindows="true" android:orientation="vertical"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/sym_def_app_icon" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginTop="8dp"/> ... </LinearLayout>
- First you need to add
android:fitsSystemWindows="true" to the layout. - Now you need to make the top
fitsSystemWindows smaller, as fitsSystemWindows will automatically add the size of the status bar. Thus, previously your top addition was from the top of your header, now it is only at the bottom of the status bar. - And you need to move all your paddings from the layout somewhere else (for example, I moved them to fields on child views), because
fitsSystemWindows will overwrite these shims.
After that, if your application is at the bottom of the multi-window split, the addition of the status bar will not be added. It will also make your navigation view valid in any other cases when it is not in the status bar or if the status bar changes size in any future version of Android or some crazy custom ROM.
source share