Search code examples
androidandroid-layoutandroid-relativelayout

android:layout_alignBottom not working for API 18 and below?


Need some enlightenment if this is different Android version issue (v18 SDK and before).

Below is the layout we have, in particular the txt_subitem should be align bottom of img_picture. The code below works fine for v19 and later (since Kit-Kat), but not for v18, as shown in picture beneath.

<RelativeLayout
    android:id="@+id/img_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/img_picture"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:src="#0f0"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_margin="20dp"/>

    <TextView
        android:id="@+id/txt_subitem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/img_picture"
        android:text="testing"
        android:layout_alignBottom="@+id/img_picture"
        android:background="#00f"
         />

</RelativeLayout>

The below shows what v19 SDK display. E.g. the blue color is aligned with the green box, which is expected due to android:layout_alignBottom="@id/img_picture" v18

However, for v18 SDK and before (tested on v17 and v16 through emulator and real devices), the image is layout where the blue color box is not aligned as expected, but alignment extend to the margin (20dp) of @+id\img_picture.

v19

Could someone shed some light if this is the Android v18 SDK issue or I have missed something. Thanks!


Solution

  • For API 18 and earlier, the margin is being applied after the alignment so if the margin in the ImageView is greater than 0 you will get your textview moved. You can check this setting the margin to 0.

    If you want to add a margin from his parent, try this:

    • Keeping android:layout_margin="20dp", add android:layout_marginTop="10dp" to your TextView
    • Not keeping android:layout_margin="20dp", use padding in the parent view (like the example).

      <RelativeLayout
          android:id="@+id/img_container"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="20dp">
      
          <ImageView
              android:id="@+id/img_picture"
              android:layout_width="100dp"
              android:layout_height="80dp"
              android:src="#0f0"
              android:layout_alignParentLeft="true"
              android:layout_centerVertical="true"/>
      
          <TextView
              android:id="@+id/txt_subitem"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBottom="@id/img_picture"
              android:layout_alignLeft="@id/img_picture"
              android:text="testing"
              android:background="#00f"/>
      
      </RelativeLayout>