Place two multiline TextViews with wrap_content width on both sides of the parent view
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>
Have you tried Flexbox layout? Looks like it covers your case.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="left" />
<TextView
android:id="@+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/left"
android:gravity="right" />
</RelativeLayout>
note that left
TextView
may occupy whole available space if got very long text, so you may add some maxWidth
param - if its not supported by TextView
itself then you may wrap first one into LinearLayout
(which supports maxWidth
for shure). another way may be to use TableLayout
and TableRow
views or ConstraintLayout