Removing the top navigation bar in Android N Multi-Window mode

I would like to remove paddingTop / marginTop from the navigation view in multi-window mode of Android N. Like in Gmail. If you see the image below, I'm talking about normal filling with a size equal to the status bar at the beginning of the navigation view.

enter image description here

So, basically in Multi-Window mode (see image below), I have to remove this add-on when my application is in the second part of the screen.

enter image description here

Unfortunately, from the new api 24 you have isInMultiWindowMode() , but it is impossible to find out which part of the screen your application is in.

+5
source share
2 answers

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.

+2
source

Nothing works for me, so I finished this route and it did the job:

 <android.support.design.widget.NavigationView ... app:insetForeground="@null"/> 

Technically, the inserts are still present, but since the insetForeground resource used to draw on them is now null, the logic is skipped in ScrimInsetsFrameLayout using the onDraw method (which is the parent class of NavigationView).

Therefore, when all else fails, this is a pretty efficient route.

0
source

All Articles