1

I am using a WearableDrawerLayout, and testing on a emulator with a chin. I am trying to have an element vertically centered. Instead what I see is that the element is centered in the area of "the screen minus the chin" - i.e. it is a bit shifted towards the top of the screen.

What I see:

enter image description here

What I should see:

enter image description here

From what I can tell in the (non public?) source of WearableDrawerLayout I think this is due to this bit:

public WindowInsets onApplyWindowInsets(WindowInsets insets) {
    this.mSystemWindowInsetBottom = insets.getSystemWindowInsetBottom();
    if(this.mSystemWindowInsetBottom != 0) {
        MarginLayoutParams layoutParams = (MarginLayoutParams)this.getLayoutParams();
        layoutParams.bottomMargin = this.mSystemWindowInsetBottom;
        this.setLayoutParams(layoutParams);
    }

    return super.onApplyWindowInsets(insets);
}

What can I do to not have this problem?

Edit: here is another example of layout that demonstrates the issue:

enter image description here

As you can see, the chin is not included in the available area, which means the BoxInsetLayout has a height smaller than it should. As a consequence, its button children are too "high" - they're not bottom aligned.

Here's my edit (sorry about my Gimp skills), that shows the round display, and also where the BoxInsetLayout and buttons should go.

enter image description here

BoD
  • 10,838
  • 6
  • 63
  • 59

2 Answers2

3

There are a few ways to accomplish this. Here's a quick and easy one...

First, the layout that I used for testing:

<android.support.wearable.view.BoxInsetLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/box">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/close_button"
            android:layout_centerInParent="true"
            android:background="#0000"/>
    </RelativeLayout>
</android.support.wearable.view.BoxInsetLayout>

It's a minimal example based on BoxInsetLayout, but the principle should scale to more complex layouts. I'm simply using a RelativeLayout for easy centering within the screen, and drawable/close_button was just a nice round graphic I had sitting around.

As is, the above layout should center in any square or fully round screen:

fully round screen

To also center it in a "flat tire" screen, we just need to tweak the root layout a bit. Here's my Java code to do so:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    DisplayMetrics metrics = getResources().getDisplayMetrics();
    findViewById(R.id.box).getLayoutParams().height = metrics.widthPixels;
}

It's crude but effective: set the height of the BoxInsetLayout equal to the screen's width. The layout will then center within that height. Here it is on a "flat tire" screen:

flat tire screen

Of course, you'll need to leave sufficient room at the bottom of your layout that your content isn't cropped, but that's unavoidable with a screen that's "missing" an area at the the bottom. If you have any elements using android:layout_alignBottom, you may need to compensate their position manually, or find another way to position them.

Sterling
  • 6,365
  • 2
  • 32
  • 40
  • This trick works, thanks a lot for it! I still think WearableDrawerLayout's behavior is incorrect regarding the chin - I'm not sure if you agree (I think I'll file an issue). But I'm glad there is a solution :) – BoD Feb 13 '17 at 17:53
0

You can use a ConstraintLayout to create a square layout.

Essentially, you constrain the left, right and top edges of your view to the edges of the screen. Then, you constrain the dimension ratio of your view to 1:1. The layout will satisfy your left/right/top constraints, and then try to satisfy the aspect ratio constraint, and the only way to move is down. So, you have a square layout.

For example,

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <View
        android:id="@+id/your_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintDimensionRatio="1:1" />
</android.support.constraint.ConstraintLayout>
Vasiliy Kulakov
  • 5,401
  • 1
  • 20
  • 15