Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are WindowInsets?

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.

like image 265
Samvid Mistry Avatar asked Nov 07 '15 16:11

Samvid Mistry


People also ask

What is WindowInsets?

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.

What is android fitsSystemWindows?

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.


2 Answers

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:

enter image description here

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 WindowInsets.

More resources:

  • Windows Insets + Fragment Transitions by Chris Banes
  • WindowInsets - Listeners to layouts by Chris Banes
like image 96
azizbekian Avatar answered Oct 04 '22 01:10

azizbekian


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.

like image 45
Henry Avatar answered Oct 04 '22 01:10

Henry