Home » Android » android – Positioning a view under two views (or the lowest view)

android – Positioning a view under two views (or the lowest view)

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a RelativeLayout with an ImageView on the left and then a TextView on the right. The TextView is downloaded from a website through their API so it’s contents will be different each time.

I want to have another TextView underneath these two but I am having a problem when the TextView length is less than the ImageView. When this happens, the TextView at the bottom will overlap the ImageView because I align the bottom TextView to be below the TextView on the top right.

What I need to happen is to be able to align the bottom TextView under whichever view is the lowest.

This is my layout XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/itemImageView"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/id_image" />

    <TextView
        android:id="@+id/itemContentsTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/itemImageView"
        android:layout_marginRight="2dp"
        android:layout_marginTop="2dp"
        android:text="Sample contents\nSample contents\nSample contents" />

    <TextView
        android:id="@+id/itemIdTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/itemContentsTextView"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:text="1234" />

</RelativeLayout>
How to&Answers:

You should create LinearLayout as top parent and make it orientation:vertical. Then first add your relativeLayout and then add your TextView.

So it will looke like this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >
    <RelativeLayout    
               android:layout_width="match_parent"
               android:layout_height="match_parent" >

        <ImageView
                android:id="@+id/itemImageView"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="5dp"
                android:src="@drawable/id_image" />

        <TextView
                android:id="@+id/itemContentsTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@+id/itemImageView"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:text="Sample contents\nSample contents\nSample contents" />
    </RelativeLayout>
    <TextView
            android:id="@+id/itemIdTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:text="1234" />
</LinearLayout>

Answer:

Wrap the top ImageView and TextView in another RelativeLayout and use that as anchor for your bottom TextView.

Something like this (not tested):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

        <ImageView
            android:id="@+id/itemImageView"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/id_image" />

        <TextView
            android:id="@+id/itemContentsTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/itemImageView"
            android:layout_marginRight="2dp"
            android:layout_marginTop="2dp"
            android:text="Sample contents\nSample contents\nSample contents" />
    </RelativeLayout>

    <TextView
        android:id="@+id/itemIdTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/wrappingLayout"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:text="1234" />

</RelativeLayout>

Answer:

Use one more RelativeLayout i have Tested and text Never overlaps

so it Should be :

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

    <ImageView
        android:id="@+id/itemImageView"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/itemContentsTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/itemImageView"
        android:layout_marginRight="2dp"
        android:layout_marginTop="2dp"
        android:text="Sample contents\nSample contents\nSample contents" />
</RelativeLayout>

<TextView
    android:id="@+id/itemIdTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/temp"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="5dp"
    android:text="1234" />