tags:

views:

107

answers:

2

I have a horizontal LinearLayout, inside which I have 2 TextViews. Let's say that the LinearLayout's width is 320px. If the TextViews don't fit into the LinearLayout (they are together wider than 320px), I want to somehow achieve this:

  • The second TextView is fully displayed and is at the right edge of the LinearLayout
  • The first TextView is only shown partially, only first x characters are visible

What I mean:

[TextView1|TextView2_____________] // this is normal

[VeryVeryL...|VeryVeryLongTextView2] // VeryVeryLongTextView1 is not fully visible

A: 

Specify a specific width for your first textView (i.e, 20dp... note, it is better to use dp than hard coded pixels, to deal with multiple resolutions of devices), give your 2nd TextView a weight of 1. This tells it to take up the remaining space. For example:

<LinearLayout ...>
    <TextView android:layout_width="20dp"
                android:layout_height="wrap_content"/>
    <TextView android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
</LinearLayout>
Mayra
and if you want the 3 dots in the text of textView1 showing that it's a long text, you can add android:ellipsize="end"
ccheneson
Thanks, but I need that if the first TextView is very small, the second one is right next to it - I want [T1|TextView2___] and not [T1___|TextView2__].
fhucho
A: 

To get the effect you're requesting in the comments above, you could modify Mayra's solution to something like:

<LinearLayout ...>
<TextView android:layout_width="wrap_content"
            android:maxWidth="20dp"
            android:layout_height="wrap_content"/>
<TextView android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

I think that will work. Weirdly, the maxWidth param is only present on a couple view classes, but TextView luckily is one of them. You'd think it'd be useful in more cases, so I'm not sure why it's not just available in the default view params.

Matt Hall
Thanks very much!
fhucho