Skip to content

Instantly share code, notes, and snippets.

@Pkmmte
Last active March 25, 2024 13:55
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pkmmte/d8e983fb9772d2c91688 to your computer and use it in GitHub Desktop.
Save Pkmmte/d8e983fb9772d2c91688 to your computer and use it in GitHub Desktop.
A FrameLayout subclass that dispatches WindowInsets to its children instead of adjusting its padding. Useful for Fragment containers.
package com.pkmmte.widget;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.WindowInsets;
import android.widget.FrameLayout;
/**
* A FrameLayout subclass that dispatches WindowInsets to its children instead of adjusting its padding.
* Useful for Fragment containers.
*
* @author Pkmmte Xeleon
*/
public class WindowInsetsFrameLayout extends FrameLayout {
public WindowInsetsFrameLayout(Context context) {
super(context);
}
public WindowInsetsFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public WindowInsetsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public WindowInsetsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@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);
return insets;
}
}
@IlyaEremin
Copy link

amazing, thank you!

@sevar83
Copy link

sevar83 commented Jan 30, 2016

Hi, any example how to use it? Because simply replacing my old FrameLayout with this class doesn't seem to work for me.

@Pkmmte
Copy link
Author

Pkmmte commented Feb 16, 2016

@sevar83 I don't really have any samples to show. As long as your parent layouts and this layout all have fitsSystemWindows="true", it should work fine.

@anukools
Copy link

thanks @Pkmmte works like a champ.. :)

@givotnoe
Copy link

what about preloli? ViewCompat?

@zJMK
Copy link

zJMK commented May 21, 2019

Thanks for sharing! It did not fix my specific problem, but for whoever might be interested, this is the Kotlin version:

class WindowInsetsFrameLayout @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) {
    @TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
    override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets {
        val childCount = getChildCount()
        for (index in 0 until childCount)
            getChildAt(index).dispatchApplyWindowInsets(insets)

        return insets
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment