I have a TextView
and an ImageView
square that I would like to show in a horizontal linear layout. Each of them should take half the width of the representation of the parent element, and the height should correspond to the content (i.e. the image). The text should be centered vertically.
An additional limitation is that the image should not grow beyond the specified maxWidth
(= maxHeight
), and excess width should be available for the TextView
. Obviously, this is contrary to rule 50/50 above. Is there a way to prioritize constraints i.e. Allow the image to take half the space if it does not exceed a given size?

These are the layouts I tried:
The first of them perfectly stretches the left side to the available space. But the image takes up more than half the width, because its layout_weight
not specified (as shown in the figure below).
<LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="vertical" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Some text."/> </LinearLayout> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxWidth="200dp" android:adjustViewBounds="true" android:src="@drawable/image" /> </LinearLayout>

When I add layout_weight
to the ImageView
, it always takes half the width and ignores maxWidth
(see the figure below).
<ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:maxWidth="200dp" android:adjustViewBounds="true" android:src="@drawable/image" />

Including ImageView
in another LinearLayout
again includes maxWidth
, but the included LinearLayout
still takes up half the available space (see image below).
<LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right"> <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:maxWidth="200dp" android:adjustViewBounds="true" android:src="@drawable/image" /> </LinearLayout>

Another option I found for such scenarios is to use a RelativeLayout
with an invisible view as the center separator, but this blocks 50/50 division (or wherever the separator is located).