Android layout using layout_weight, layout_width and maxWidth

Blundell picture Blundell · Feb 10, 2011 · Viewed 8.9k times · Source

I'm trying to get a row of text which will be something like

foofoofoo - barbarbar

but I want it to ellipse foo and bar if it won't fit on one line. i.e. I'm trying to shorten them down.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxWidth="0dip"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:singleLine="true"
        android:ellipsize="true"
        android:text="foofoofoofoo" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:text=" - " />
    <TextView
        android:id="@+id/text_2"
        android:layout_width="wrap_content"                         
        android:layout_height="wrap_content"
        android:maxWidth="0dip"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:singleLine="true"
        android:ellipsize="true"
        android:text="barbarbarbar" />
</LinearLayout>

Bear with me this works partially.

Setting the TextView's layout_width to be 0dip and layout_weight to be 1 means they will take up 50% of the available space each.

Setting the singleLine to true and ellipsize to true means they will look like foofoo... if the text is larger than the container.

Therefore my outcome (if the text is longer) should be

foofoo.. - barbar..

Which it is! So this works.

Now the case I'm trying to fix is, if the first TextView (id:text_1) has text that is less than the 50% given by layout_width="0dip" and layout_weight="1" I want it to wrap_content. Otherwise it looks like:

foo blank space - barbar..

and I want

foo - barbarbar

This is why I have changed layout_width="wrap_content" and added android:maxWidth="0dip" but this doesn't work! It seems to be ignoring me saying wrap_content and still giving this view 50%!

I read that you need to add android:adjustViewBounds="true" for maxWidth to work but this had no affect.

Answer

bigstones picture bigstones · Mar 9, 2011

This is the best I could do, try changing the strings to see if it works as intended.

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:shrinkColumns="0,2"
    android:stretchColumns="0,2">
    <TableRow>
        <TextView android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:text="foofoofoo"/>
        <TextView android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"
            android:gravity="center_horizontal"/>
        <TextView android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:text="barbarbarbarbarbarbarbarbarbarbarbar"/>
    </TableRow>
</TableLayout>