M.A.Murali M.A.Murali - 1 month ago 9
Android Question

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

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

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" />