This issue relates to expanding elements with screen width, but only up to a maxWidth.
My scenario involves a vertically oriented LinearLayout
containing a fixed sequence of rows. Each of those rows is a horizontal LinearLayout containing some EditText
elements, separated by TextView
elements. The TextView elements' text contains a mathematical symbol (e.g. + or - or /) and are meant to indicate that the contents of second box will be added to (or subtracted from, etc) the first box, for display elsewhere in the UI.
The EditText
elements must stay their current fixed width, but I want the TextView
elements (acting like separators) to expand in width if the screen will allow it, but only up to a maxWidth of 30dp. Currently the layout for one example row looks like:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="@dimen/row_side_padding"
android:paddingRight="@dimen/row_side_padding"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<EditText
android:id="@+id/editTextBoxOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/box_one_hint"
android:imeOptions="actionDone"
android:inputType="numberSigned"
android:maxLines="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/plus" />
<EditText
android:id="@+id/editTextBoxTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/box_two_hint"
android:imeOptions="actionDone"
android:inputType="numberSigned"
android:maxLines="1" />
</LinearLayout>
This current code wraps the width of the TextView to the width of the text (just a plus symbol in this example). This is exactly what I want on small screens!
However, on wider screens it would be nice if the TextView grew wider to space out the elements a bit. I am able to expand the TextView by setting it as:
<TextView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/plus" />
But this makes the TextView too large. I want it to expand to a maximum of 30dp, if the screen width allows it. But from what I understand, android:maxWidth
does not work with android:layout_width="match_parent"
so any maxWidth is just ignored.
So how can I expand my TextView based on screen width, but only up to a maximum width, while leaving my EditText elements the same size?