Search code examples
androidandroid-layoutwear-os

BoxInsetLayout doesn't work


I created sample android wear activity project in android studio v1.2.2. I deleted WatchViewStub and used this example to create activity with BoxInsetLayout. But BoxInsetLayout doesn't work correctly on Round Device. And I tested this on moto 360 android 5.1.1 and before update on moto 360 version 5.0.1. And I tested this on emulator. And It doesn't work at all. All time I see this:

but must be this

My code below:

activity_main.xml

<android.support.wearable.view.BoxInsetLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:padding="15dp">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:background="@color/red"
        app:layout_box="all">

        <TextView
            android:gravity="center"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="Hello Android Wear"
            android:textColor="@color/black" />

        <ImageButton
            android:background="@drawable/selector_btn_ok"
            android:layout_gravity="bottom|start"
            android:layout_height="50dp"
            android:layout_width="50dp" />

        <ImageButton
            android:background="@drawable/selector_btn_cancel"
            android:layout_gravity="bottom|end"
            android:layout_height="50dp"
            android:layout_width="50dp" />
    </FrameLayout>
</android.support.wearable.view.BoxInsetLayout>

Why doesn't it work? Where did I mistake? And How can I use correctly BoxInsetLayout on devices with Round screen?


Solution

  • The mistake is in your activity_main.xml, where you specified:

    android:padding="15dp"
    

    If you remove this, then it should work. BoxInsetLayout uses padding in the implementation, and you have changed the value which gives the incorrect output you observed.

    (edit) It is important to note that BoxInsetLayout adjusts the padding in both itself, and also the child views. So make sure you do not change the padding values or things will break. You can try embedding a second FrameLayout if you want to have more control over the padding.