Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid layout using layout_weight, layout_width and maxWidth
    text
    copied!<p>I'm trying to get a row of text which will be something like</p> <p>foofoofoo - barbarbar</p> <p>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.</p> <pre><code>&lt;LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"&gt; &lt;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" /&gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:text=" - " /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Bear with me this works partially.</p> <p>Setting the <code>TextView</code>'s <code>layout_width</code> to be <code>0dip</code> and <code>layout_weight</code> to be <code>1</code> means they will take up 50% of the available space each.</p> <p>Setting the <code>singleLine</code> to <code>true</code> and <code>ellipsize</code> to <code>true</code> means they will look like <em>foofoo...</em> if the text is larger than the container.</p> <p>Therefore my outcome (if the text is longer) should be</p> <blockquote> <p><em>foofoo.. - barbar..</em></p> </blockquote> <p>Which it is! So this works.</p> <p><strong>Now the case I'm trying to fix is</strong>, if the first <code>TextView</code> (id:text_1) has text that is less than the 50% given by <code>layout_width="0dip"</code> and <code>layout_weight="1"</code> I want it to <code>wrap_content</code>. Otherwise it looks like:</p> <blockquote> <p><em>foo</em> <strong>blank space</strong> <em>- barbar..</em></p> </blockquote> <p>and I want</p> <blockquote> <p><em>foo - barbarbar</em></p> </blockquote> <p>This is why I have changed <code>layout_width="wrap_content"</code> and added <code>android:maxWidth="0dip"</code> but this doesn't work! It seems to be ignoring me saying <code>wrap_content</code> and still giving this view 50%!</p> <p>I read that you need to add <code>android:adjustViewBounds="true"</code> for <code>maxWidth</code> to work but this had no affect.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload