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:
please help me.
View Height and Width This can take the form of wrap_content (adjust height and width to the content size), match_parent (adjust height and width to the full size of the parent container), and a dimensions value such as 120dp . This can be changed at runtime in a number of ways: Java. Kotlin.
There are two different ways to set the height and width of component: Fixed Dimensions and Flex Dimensions.
Widgets dimension constraints Using 0dp , which is the equivalent of " MATCH_CONSTRAINT "
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" />
If you're using a Layout other than a RelativeLayout, you can match the widths of your widgets programmatically, such as in this example:
layout.xml:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Here's some text" /> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Some more text" /> </LinearLayout>
Notice that both text fields are both set to wrap_content.
Main.java:
TextView tv1 = (TextView) findViewById(R.id.text1); TextView tv2 = (TextView) findViewById(R.id.text2); if(tv1.getWidth() < tv2.getWidth()) tv1.setWidth(tv2.getWidth()); else tv2.setWidth(tv1.getWidth());
If you have multiple widgets that you want to have a uniform width, just repeat the above code for the new element. For example, let's say there was a button I wanted to adjust the width to, to match the other elements:
if(tv2.getWidth() < button) tv2.setWidth(button.getWidth()); else button.setWidth(tv2.getWidth());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With