Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place two multiline TextViews with wrap_content width on both sides of the parent view

Help me please achieve following arrangement of two TextViews:

enter image description here

  1. On the left is a simple TextView with title, and on the right is another TextView with a Drawable on the left side of it (it is important because of it I can’t use match_parent). Both TextViews should be wrap_content and pressed to their side.
  2. If one of the TextViews is too long, it should rest against another TextView and wrap its own text.
  3. If both TextViews are long, then they should take up the same space. None of them should be pushed outside the parent view.
like image 890
Nikolay Avatar asked Nov 09 '20 06:11

Nikolay


People also ask

What is Wrap_content?

Wrap Content: Wrap content takes the length of the text and wraps its content. Example: “This is a Wrap Text”. The wrap content starts wrapping from “This” and ends at “text”.

What is multiline text in Android Studio?

Posted on Apr 28, 2022. Learn how to create a multiline TextView in Android application. The Android TextView widget is used to display text in your application. When you have a text that's longer than one line, then the TextView will automatically put the text in multiple lines.


2 Answers

You can use both LinearLayout or ConstraintLayout, and the main here is android:maxWidth this we need to give one view and the other view will get adjust based on that.

ConstraintLayout example

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/start"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toStartOf="@+id/end"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Loooooooooong Texxxxxxxt!!!!!!!!!!!!" />

    <TextView
        android:id="@+id/end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="250dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="May be this is short"/>

</androidx.constraintlayout.widget.ConstraintLayout>

LinearLayout Example:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Loooooooooong Texxxxxxxt!!!!!!!!!!!!!!!!!!!!!!" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="250dp"
        android:text="May be this is short with maxwidth"/>

</LinearLayout>
like image 96
Muthukrishnan Rajendran Avatar answered Sep 28 '22 06:09

Muthukrishnan Rajendran


Have you tried Flexbox layout? Looks like it covers your case.

like image 43
kronshtatov Avatar answered Sep 28 '22 07:09

kronshtatov