Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine how much text will fit in a TextView in Android?

I have a layout that looks something like this:

[TextView 1] [TextView 2]
[ TextView 2 spill-over ]

Essentially, I need the contents of TextView 2 to wrap to the next line, but start where TextView 1 starts. I was thinking that if I knew how much text would fit into TextView 2 before it runs out of space on line one, I could take the rest of the text and put it in another TextView below the first two. So I need to measure how much text will fit into a TextView (which can be tricky because as far as I can tell, Android will try to break the text in a TextView at a good location so that it won't break a word in the middle if it can be avoided) or I need a new idea on how to lay this out.

Any help would be greatly appreciated.

Thanks in advance,

groomsy

like image 699
groomsy Avatar asked Aug 04 '10 19:08

groomsy


1 Answers

Unfortunately, Paint.breakText didn't return the exact same result as in was seen in my two-line TextView.

However, this worked

int numChars = textView.getLayout().getLineEnd(1);

(use numberOfLines - 1 as the parameter to it)

Ref http://developer.android.com/reference/android/text/Layout.html#getLineEnd(int) Set individual lines of TextView to different width

like image 128
dparnas Avatar answered Sep 28 '22 01:09

dparnas