5

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.

Luuklag
  • 3,897
  • 11
  • 38
  • 57
Blundell
  • 75,855
  • 30
  • 208
  • 233

2 Answers2

10

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>
bigstones
  • 15,087
  • 7
  • 65
  • 82
2

It is rather hard to tell exactly what you want, but I believe the answer is you can't do this unless you dynamically adjust your layout in code. The reason for this is because you are asking the system to work in multiple different ways.

First, you want the views to share the space equally when the amount of text in the view is the same.

Second, you want the views to NOT share the space equally when there is less text in the other view.

There are multiple other cases here that can be handled any way you want. What I'm trying to convey however, is that your asking the same layout to handle things in different ways which is why you aren't able to achieve this with a single layout.

user432209
  • 20,007
  • 10
  • 56
  • 75
  • I want to basically say maxWidth is share the space (therefore elipsing), but normally just wrap_content. So I would assume it would wrap_content unless wrap_content's dip value > 50% then it would use maxWidth (which is the 50%), no? – Blundell Feb 10 '11 at 20:25
  • `unless wrap_content's dip value > 50% then it would use maxWidth`... I'm really not sure what your trying to say. `wrap_content` doesn't use a dip value. – user432209 Feb 10 '11 at 23:38
  • Yes it doesn't but maxWidth has to interact with layout_width in some way. So I meant in the background it must calculate the layout's width and if it is > than maxWidth use maxWidth. So I'm sayin wrap_content , but if (in the background) wrap_content is > 50% use maxWidth. Is this not how it works? – Blundell Feb 11 '11 at 09:27
  • I think I understand what your getting at. The problem is the "theoretical dip value" of wrap_content changes depending on, in this case, the text in the view. You simply can't come up with one layout that will work in all situations, like your trying to do. See my original answer. – user432209 Feb 12 '11 at 16:32
  • I don't see why layout_width="wrap_content" & maxWidth="0dip" wouldn't work :-( . In english this would be 'Wrap the content with a maxWidth of 50%' – Blundell Mar 07 '11 at 14:28