Search code examples
androidandroid-relativelayout

Wrap content is not working on Vertical line in Relative Layout?


I am using View with a background colour to create a vertical line in a Relative layout . Wrap content is not working , the line is taking the entire viewport as height. Why is this happening ? attaching my code which is not working

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f5f5f5"
android:orientation="vertical"
tools:ignore="MissingPrefix">

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <View
        android:layout_width="2dp"
        android:layout_alignParentTop="true"
        android:layout_height="wrap_content"
        android:layout_marginLeft="21.5dp"
        android:background="#a9382b" />

    <TextView
        android:id="@+id/seeall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dp"
        android:textColor="#a9382b"
        android:textSize="11sp"
        tools:text="SEE ALL" />

</RelativeLayout>

</LinearLayout>

Solution

  • Setting the height to wrap_content in this case simply will not work.

    In general, wrap_content will give the view enough size to contain everything "inside" it (whether that's child views or just the text in a TextView, etc), but your View tag doesn't have anything "inside" it to define the height. In this case, it will grow to fill all available space.

    If you want your RelativeLayout to be as tall as the TextView inside of it, and you want the line to go from top to bottom of that size, use the following for your View tag:

        <View
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:layout_alignTop="@+id/seeall"
            android:layout_alignBottom="@+id/seeall"
            android:layout_marginLeft="21.5dp"
            android:background="#a9382b">