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:
What I should see:
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:
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.
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:
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:
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.