Kyle
Kyle

Reputation: 618

In Android, what are window insets?

This sounds like a dumb question, and I'm sorry if it is, but I've searched around to get a visual representation of what they are and came up with nothing.

Here is what Android has to say about it:

WindowInsets are immutable and may be expanded to include more inset types in the future.

http://developer.android.com/reference/android/view/WindowInsets.html

Google images autocorrect it to window inserts...

Why would anyone work with these "insets"? Do they have anything to do with the navigation bar on mobile phones with no home physical keys?

Upvotes: 21

Views: 12508

Answers (5)

Allan Veloso
Allan Veloso

Reputation: 6369

Insets are areas of your view that you should not put elements, like behind the status bar or navigation bar. Think of them like paddings for the window.

If you want to draw behind them, like putting an image to the top that should be behind a translucent status bar, you will need to consume the window insets. In some views this is as easy as putting android:fitsSystemWindows=true, but in others you will have to override the onApplyWindowInsets method.

Usually the window insets for phones are the size of the height of the status bar as the top, the size of the navigation bar as the bottom and 0 as left and right. But It can be different, like in watches or phones with physical buttons.

Upvotes: 9

Aksenov Vladimir
Aksenov Vladimir

Reputation: 707

I wrote sample.

This repository can help to grasp android windows insets idea and Edge-To-Edge concept + we can detect keyboard without assumption that "keyboard should be more than X part of screen"

enter image description here

enter image description here

enter image description here

Upvotes: 2

Davide Cannizzo
Davide Cannizzo

Reputation: 3134

WindowInsets is a class that represents the so-called window insets. They're some kind of margins/paddings your content should have not to overlap unusable areas of the screen. As many people suggested, they can be used to detect whether an Android Wear device is round and handle that shape. However, the probably most common case of use is on mobile phones. Status and navigation bars are both part of the System UI, and thus they cover different rooms of the screen you should not place your content in. An Activity, depending on the window flags set in, can either use the whole screen or be resized below the status bar and above the navigation bar (see WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN and WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS). So, if you set your activity not to handle window insets, you'll have to do it all by yourself. And you would use View.setOnApplyWindowInsetsListener method to let yourself notice whenever those insets change, and you would handle UI changes (e.g. set some padding) based on the values provided in the listener.

Upvotes: 2

Evin1_
Evin1_

Reputation: 12866

They are some kind of colored margin (used in Android Wear).

They are used to create a padding from the main content to the actual border:

There are a few examples here.


This is an image with 2 insets: Circle/Squared.

enter image description here


They can also be used in other views to handle especial rendering requirements, like in a ScrollView: where to put the actual scroll can be defined with an insideInset as mentioned in this question.

<ScrollView
    android:id="@+id/view2"
    android:layout_width="100dip"
    android:layout_height="120dip"
    android:padding="8dip"
    android:scrollbarStyle="insideInset"
    android:background="@android:color/white"
    android:overScrollMode="never">

Upvotes: 7

marcinj
marcinj

Reputation: 49986

You may use onApplyWindowInsets:

@Override
public void onApplyWindowInsets(WindowInsets insets) {
    super.onApplyWindowInsets(insets);
    mRound = insets.isRound();
}

to detect if wearable android device is round or square, then using that information draw appropriate application interface (with round or square background)

Upvotes: 3

Related Questions