Android: how to force left justified items to wrap rather than right justified items?
-
21-04-2021 - |
Question
this is the layout i have:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight">
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:id="@+id/customer_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:paddingLeft="4dip"/>
<TextView
android:id="@+id/ticket_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:paddingLeft="6dip"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="right">
<TextView
android:id="@+id/total"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:paddingRight="6dip"/>
<TextView
android:id="@+id/date"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:paddingRight="6dip"/>
</LinearLayout>
</LinearLayout>
which produces list items that look like this:
and as you can see, it's a bit ugly. i'd like for the top blue box to wrap if the top right box is red, and have the bottom row completely independent of the justification up top, such that the date does not wrap at all, regardless of what the top row looks like. what's the best way to accomplish this?
Solution
Use a TableLayout
and make it have shrinkColumns="0"
and specifify the client name/ticket# to be in column 0. You can also set strechColums for ones you want to expand to fit, so probably column 1 to expand the column for dates.
Other option is to not put them in vertical linear layouts and instead make horizontal ones for each row.
OTHER TIPS
implementation:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:shrinkColumns="0"
android:stretchColumns="1"
android:minHeight="?android:attr/listPreferredItemHeight">
<TableRow>
<TextView
android:id="@+id/customer_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_span="2"
android:textSize="18sp"
android:paddingLeft="4dip"/>
<TextView
android:id="@+id/total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:gravity="right"
android:layout_gravity="right"
android:textSize="18sp"
android:paddingRight="6dip"/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/ticket_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:textSize="14sp"
android:paddingLeft="6dip"/>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_span="2"
android:layout_gravity="right"
android:gravity="right"
android:textSize="14sp"
android:paddingRight="6dip"/>
</TableRow>
</TableLayout>