KevinTydlacka
KevinTydlacka

Reputation: 1293

How to use setOnApplyWindowInsetsListener for Android Wear

I'm trying to detect when my app is run on a round Wear device, but I can't get the OnApplyWindowInsetsListener to work from this post.

Can I just attach this to any view object in my layout, or my layout itself? When is it called? I can't get the listener to fire. Any code examples would be much appreciated.

I create and add this ImageView to my main layout in my OnResume method:

backgroundImage = new ImageView(this);
    backgroundImage.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
            if (windowInsets.isRound()){
                Log.d("DEVELOPER", "......screen is round");
            }
            Log.d("DEVELOPER", "......windowinsets called");
            return null;
        }
    });

Upvotes: 3

Views: 8897

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364780

In my experience not all views call this listener.

Check also the doc: https://developer.android.com/reference/android/view/View.html#onApplyWindowInsets(android.view.WindowInsets)

Using a WatchViewStub you can easily obtain it:

WatchViewStub stub = (WatchViewStub) findViewById(R.id.stub);
stub.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
            @Override
            public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {

                if(insets.isRound()) {
                    // Yes device is round
                } 
                return null;
            }
        });

Here the layout used:

<android.support.wearable.view.WatchViewStub
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/stub"
        app:rectLayout="@layout/rect_layout"
        app:roundLayout="@layout/round_layout"/>

Upvotes: 1

Related Questions