Last active
August 12, 2024 07:40
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Hi, any example how to use it? Because simply replacing my old FrameLayout
with this class doesn't seem to work for me.
@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.
thanks @Pkmmte works like a champ.. :)
what about preloli? ViewCompat?
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
amazing, thank you!