Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the android text view to cut any letters that don't fit in a layout

I have several TextViews in a row and I want the 1st TextView to take up all spare space but if the combined length of the TextViews is too long to fit on one line, I want the 1st TextView to be cut off.

As for the spare space I have set the 1st TextView's layout_width to match parent and layout_weight to 1 which works. I just can't get it to cut off any extra letters so it fits on one row.

What I want is something like the following with 'Text' and 'Long Text'

'Text   :data'
'Long T :data'

So 'Long Text' gets cut off to 'Long T' in order to fit.

What I get is

'Text   :data'
'Long Text :d'

Thanks!

like image 515
Dre Avatar asked Jan 15 '11 16:01

Dre


2 Answers

Give the first (left) TextView a set size (set in relative terms, dp), or use maxWidth for it, and also use ellipsize.

http://developer.android.com/reference/android/widget/TextView.html#attr_android:ellipsize

This is a good answer to your problem: TextView cuts off text when it is long enough

Also, seems similar to this question which may help: Two TextViews side by side, only one to ellipsize?

like image 129
Charlie Collins Avatar answered Nov 02 '22 18:11

Charlie Collins


Try something like this:

android:singleLine="true"
android:ellipsize="start"
android:scrollHorizontally="true"
like image 30
Bogdan Alexandru Militaru Avatar answered Nov 02 '22 18:11

Bogdan Alexandru Militaru