-1
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:textSize="24sp"
            android:text="Name."
            android:textColor="#000"
            android:textStyle="bold"
            android:background="#c91799c9"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:textSize="17sp"
            android:paddingLeft="8dp"
            android:text="Paragraph"
            android:textColor="#000"
            android:textStyle="bold"
            android:background="#3f1799c9"
            android:layout_height="wrap_content" />

    </LinearLayout>

In the given code , I am having 2 textviews in a horizontal view . I want that when text of one textview goes in another line, the another textview space should also be increased according to first one

As in given image paragraph can be in 3-4 lines, but I want Name textview should be expanded vertically

Tapesh Gupta
  • 363
  • 7
  • 21

6 Answers6

2

Try this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:textSize="24sp"
            android:text="Name."
            android:textColor="#000"
            android:textStyle="bold"
            android:background="#c91799c9"
            android:layout_height="match_parent" />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:textSize="17sp"
            android:paddingLeft="8dp"
            android:text="Paragraph"
            android:textColor="#000"
            android:textStyle="bold"
            android:background="#3f1799c9"
            android:layout_height="match_parent" />

    </LinearLayout>

</LinearLayout>

Output

enter image description here

Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
1

Try this, set android:layout_height="match_parent" in both textView also you can set Textsize same in both textView android:textSize="24sp"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">

<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#c91799c9"
    android:text="Name."
    android:textColor="#000"
    android:textSize="24sp"
    android:textStyle="bold" />

<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#3f1799c9"
    android:paddingLeft="8dp"
    android:text="Paragraph"
    android:textColor="#000"
    android:textSize="17sp"
    android:textStyle="bold" />

</LinearLayout>
Jinal Awaiya
  • 441
  • 3
  • 14
0

Try this :

Just give height to match_parent

enter image description here

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#c91799c9"
        android:text="Name."
        android:textColor="#000"
        android:textSize="24sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#3f1799c9"
        android:paddingLeft="8dp"
        android:text="paragraph"
        android:textColor="#000"
        android:textSize="17sp"
        android:textStyle="bold" />

</LinearLayout>

If you want the text in center you can add android:gravity="center_vertical" in both textview.

Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
0

Give gravity centre to the first TextView.ie add below code to the First TextView 'Name'.

android:layout_gravity="center"

And also change the height of both TextView to match_parent.

 android:layout_height="match_parent"
TheHound.developer
  • 396
  • 1
  • 3
  • 16
0

You have to set the width after the layout is shown or you will get an error, there is a command .onPost that you can use to avoid this error.

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
            <ImageView
            android:id="@+id/my_image"
            android:layout_width="wrap_content"
            android:layout_height="50dp"/>
            <EditText
            android:id="@+id/et_user"
            android:layout_width="wrap_content"
            android:layout_height="50dp"/>
            <Button
            android:id="@+id/bt_start"
            android:layout_width="wrap_content"
            android:layout_height="50dp"/>
</LinearLayout>

the java code must be places in OnCreate method but after setContentView

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView my_image = findViewById(R.id.my_image);
Button bt_start = findViewById(R.id.bt_start);
EditText et_user = = findViewById(R.id.et_user);
if ((my_image != null))
    {
        my_image.post(new Runnable()
        {
            @Override
            public void run()
            {
                bt_start.setWidth(my_image.getWidth());
                et_user.setWidth(my_image.getWidth());                                 
            }
    });
}

In my example you get the imageview width using my_image.getWidth() and set this value to other components like bt_start or et_user but you can do it in any other component.

my_image != null

can be change for any other boolean comparision like :

bt_start != null
et_user != null

this is only to confirm the item you get the width from is not null so you could get the width correctly, if this item was null you get a null point exception.

-1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    xmlns:android="http://schemas.android.com/apk/res/android">

   <TextView
       android:layout_width="0dp"
       android:layout_weight="1"
       android:textSize="24sp"
       android:text="Name"
       android:textColor="#000"
       android:textStyle="bold"
       android:gravity="center"
       android:background="#c91799c9"
       android:layout_height="match_parent" />
   <TextView
       android:layout_width="0dp"
       android:layout_weight="1"
       android:textSize="17sp"
       android:paddingLeft="8dp"
       android:text="Paragraph"
       android:gravity="center"
       android:textColor="#000"
       android:textStyle="bold"
       android:background="#3f1799c9"
       android:layout_height="match_parent" />

</LinearLayout>
Ankita
  • 1,129
  • 1
  • 8
  • 15