2

I've read some of the other posts here such as Two TextViews side by side, only one to ellipsize? but I'm still having an issue with my layout.

I have a list item layout, and I want each item in the list to look like this:

| (Expanding TextView #1) (TextView #2)                                                          (Image) |

TextView #2 and Image must always be visible.

Right now I'm using the following layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainItem"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="@drawable/myBackground"
android:onClick="onClick"
android:longClickable="true">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:layout_gravity="center_vertical|left"
    android:gravity="center_vertical|left"
    android:layout_toLeftOf="@+id/myImage"
    android:layout_alignParentLeft="true">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="14dp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="0"/>
     <TextView
        android:id="@+id/testView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>    
 <ImageView
    android:id="@+id/myImage"
    android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:layout_gravity="center_vertical"
    android:layout_alignParentRight="true"
    android:paddingRight="14dp"
    android:onClick="onClick"
    android:src="@drawable/myIcon"/>
</RelativeLayout>

I've read from the other posts that adding a layout_weight="1" to TextView#1 will force TextView #2 to be shown, and it does, but the problem is that this forces TextView #2 to be right-aligned because it causes TextView #1 to expand even when it doesn't have to.

I'm pretty stumped on this now... could anyone help? :)

UPDATE

I was able to fix this by using a TableLayout and the shrink & stretch column properties. By playing around with that it finally worked the way I wanted it to.

Community
  • 1
  • 1
Kevin
  • 85
  • 8
  • I ran into a similar issue and just had to redesign my row. I was not able to find a solution. I wish you luck! – sgarman Mar 26 '11 at 03:00

2 Answers2

1

This will truncate (if needed) the text in the first TextView, keep the text in the second TextView as is, and align and keep as is the text in the third TextView.

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="0"
        android:stretchColumns="2">

        <TableRow>

            <TextView
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLines="1"/>

            <TextView
                android:layout_height="wrap_content"
                android:maxLines="1"/>

            <TextView
                android:layout_height="wrap_content"
                android:gravity="end"
                android:maxLines="1"/>
        </TableRow>
    </TableLayout>
Jure Vizjak
  • 6,748
  • 2
  • 16
  • 15
0

If I were you I'd probably switch the row from LinearLayout to RelativeLayout, that way you can align image to the parent right, butt textview2 right up next to it and just align textview1 with the parent left and it can resize without affecting the other two fields.

NightWatchman
  • 1,217
  • 5
  • 17
  • 25
  • I tried the relative layout but my TextView2 ended up right aligned and next to the image on the right instead of next to TextView1. I didn't find a way to make it left-aligned while also maintaining the property of always being visible. I suppose I could always try "minEms" or something like that but I'd rather avoid a solution like that if I can. – Kevin Mar 28 '11 at 17:28