Friday, March 3, 2017

Android fitsSystemWindows not working when replacing fragments

Leave a Comment

I have SingleFramgnetActivity whose purpose is only to hold and replace fragments inside it.

layout looks like this:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:gravity="center_horizontal"     android:orientation="vertical"     tools:context=".SingleFragmentActivity"     >      <include layout="@layout/toolbar"/>      <FrameLayout         android:id="@+id/content"         android:layout_width="match_parent"         android:layout_height="match_parent"         /> </LinearLayout> 

I'm replacing the Fragments inside the FrameLayout. When I set the fitsSystemWindows to true on the Fragment layout, it is not responding. Actually it is working only when Activity is created, but once I replace the Fragment inside the FrameLayout, the fitsSystemWindows parameter is ignored and the layout is below the status bar and navigation bar.

I found some solution with custom FrameLayout which is using deprecated methods, but for some reason it is not working for me (same result as with normal FrameLayout) and I also do not like the idea to use deprecated methods.

1 Answers

Answers 1

Your FrameLayout is not aware of window inset sizes, because it's parent - LinearLayout hasn't dispatched him any. As a workaround, you can subclass LinearLayout and pass insets to children on your own:

@TargetApi(Build.VERSION_CODES.KITKAT_WATCH) @Override public WindowInsets onApplyWindowInsets(WindowInsets insets) {     int childCount = getChildCount();     for (int index = 0; index < childCount; index++)         getChildAt(index).dispatchApplyWindowInsets(insets); // let children know about WindowInsets      return insets; } 

You can have a look to my this answer, which will explain detailed how this works, and also how to use ViewCompat.setOnApplyWindowInsetsListener API.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment