how to set width which is equal to another widget on android

M.A.Murali picture M.A.Murali · Apr 2, 2012 · Viewed 43.6k times · Source

I need to draw a horizontal line below a text field such that the width of the line equals the text width (not the width of the full screen).

In my app I have a textview below a view(Horizontal line). The width of the line view should be equal to the width of the textview. I tried android:layout_width="wrap_content" and "match_parent", which does not solve the problem.

This is xml coding sample:

         ......
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="28dp"
            android:text="PopUpWindow"
            android:textAppearance="?android:attr/textAppearanceLarge" />


            <View
                android:id="@+id/separator"
                android:layout_width="wrap_content"
                android:layout_height="0.3dp"
                android:layout_below="@+id/textView1"
                android:background="#ffffff" />
             ......

image of the screen is:

enter image description here

please help me.

Answer

Jave picture Jave · Apr 2, 2012

If you use a RelativeLayout you can use the align-attributes:

<View
    android:id="@+id/separator"
    android:layout_width="0dp"
    android:layout_height="0.3dp"
    android:layout_below="@+id/textView1"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignRight="@+id/textView1"
    android:background="#ffffff" />