Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How do I know how many text can fit in my textView (my page)?

I want to create a text reader, which will cut the text and put it in the TextView (represents my pages) and one after the other. These are contained in a ViewFlipper (represents my entire text), which allows me to turn the pages.

By cons I can not cut my text intelligently. For now I limit each TextView to a number of characters, but it is very unstable. Indeed some letters take up more space than others .. We can not use this method.

How can we know what quantity of text will enter into a TextView?

I tried to find the maximum size of my View manually . I invoked in:

float test = view.getPaint().measureText(myString);

It works well. When I put 480 letters (0 and 1), I get to 14,400 and my text is entirely within the screen. With:

1  -> 12.0
0  -> 12.0
é  -> 11.0
\n -> 13.0
\r ->  7.0

total to fill the page -> 14400

The problem is that the line breaks and carriage return are causing problems.

If I have only letters or numbers in my string, so good: if we respect the maximum size of 14,400, everything is displayed on the screen. No Problem!

By cons, if a line breaks or a carriage return, it does not work. Even if we we respect the limit of 14400, the text above and is not fully displayed.

This is because the "MeasureText" method;" only returns 13 for a line break. But it's weird, a line break should account for at least 480 to make it consistent.

How do publishers eBook (Moon Reader, + Aldiko) for the text to be formatted so good? Each page has the same height, the text ends in the same place ..

like image 893
Kr1 Avatar asked Sep 14 '11 10:09

Kr1


1 Answers

You can fix the number of lines in your TextView by adding android:maxLines="some_integer". The other way is this:

if (myTextView.getMeasuredWidth() < myTextView.getPaint().measureText(myText)) {
    myTextView.setHorizontalFadingEdgeEnabled(true);
    myTextView.setHorizontallyScrolling(true);
}

But this will add cloudy effect to the last character if the text is too long.

like image 76
superM Avatar answered Sep 22 '22 18:09

superM