Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 897
M.A.Murali Avatar asked Apr 02 '12 13:04

M.A.Murali


People also ask

Which view attribute do you use to set the width of a view so that it adjusts to fit the content?

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.

Which are two different ways to set the height and width of component in Android?

There are two different ways to set the height and width of component: Fixed Dimensions and Flex Dimensions.

What is 0dp in constraint layout?

Widgets dimension constraints Using 0dp , which is the equivalent of " MATCH_CONSTRAINT "


2 Answers

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" /> 
like image 146
Jave Avatar answered Sep 28 '22 05:09

Jave


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()); 
like image 37
Argus9 Avatar answered Sep 28 '22 04:09

Argus9