I am trying to learn about the Android OS and while I was reading the Google I/O 2014 app, I came across the WindowInsets
. If anyone can explain what they are then it would be a great help. Thank you.
android.view.WindowInsets. Describes a set of insets for window content. WindowInsets are immutable and may be expanded to include more inset types in the future. To adjust insets, use one of the supplied clone methods to obtain a new WindowInsets instance with the adjusted properties.
That's what the default behavior of the android:fitsSystemWindows=“true” attribute gives you: it sets the padding of the View to ensure the contents don't overlay the system windows.
WindowInsets
are insets (or sizes) of system views (e.g. status bar, navigation bar), that are applied to the window.
It would be easy to understand on concrete example. Image this scenario:
Now, you don't want WindowInsets
to be applied to the background ImageView
, because in that case the ImageView
would be padded by status bar height.
But you do want insets to be applied to Toolbar
, because otherwise Toolbar
would be drawn somewhere mid status bar.
The view declares a desire to apply WindowInsets
in xml by saying:
android:fitsSystemWindows="true"
In this example you cannot apply the WindowInsets
to the root layout, because the root layout would consume WindowInsets
, and the ImageView
would be padded.
Instead you may use ViewCompat.setOnApplyWindowInsetsListener
to apply insets to toolbar:
ViewCompat.setOnApplyWindowInsetsListener(toolbar, (v, insets) -> { ((ViewGroup.MarginLayoutParams) v.getLayoutParams()).topMargin = insets.getSystemWindowInsetTop(); return insets.consumeSystemWindowInsets(); });
Note, this callback would be called, when Toolbar
's root layout passes WindowsInsets
to its children. Layouts like FrameLayout
, LinearLayout
do not, DrawerLayout
, CoordinatorLayout
do.
You can subclass your layout, e.g. FrameLayout
and override onApplyWindowInsets
:
@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; }
There's a nice blog post at medium by Ian Lake concerning this stuff, also "Becoming a master window fitter🔧" presentation by Chris Banes.
I've also created a detailed article at Medium concerning WindowInset
s.
More resources:
You can learn all about WindowInsets here. WindowInsets
provides you with the area on the window that is usable by the application. By itself it's of not much use. It's true purpose comes when you either override View.onApplyWindowInsets
or implement View.OnApplyWindowInsetsListener
. You can read about them here: View.onApplyWindowInsets and View.OnApplyWindowInsetsListener.
Listener for applying window insets on a view in a custom way.
Apps may choose to implement this interface if they want to apply custom policy to the way that window insets are treated for a view. If an OnApplyWindowInsetsListener is set, its onApplyWindowInsets method will be called instead of the View's own onApplyWindowInsets method. The listener may optionally call the parameter View's onApplyWindowInsets method to apply the View's normal behavior as part of its own.
In short, overriding this will let you control area of the window available for your View.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With