12

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 ..

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Kr1
  • 1,269
  • 2
  • 24
  • 56

2 Answers2

2

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.

superM
  • 8,605
  • 8
  • 42
  • 51
  • Thank you but I can not use your code. The "getMeasuredWidth" does not seem appropriate. She returns 0 every time. Thank you for the "myTextView.getPaint (). MeasureText (myText)" is exactly that I need. Now I only need a method to determine the maximum capacity of my View. "getMeasuredWidth" does not return anything. – Kr1 Sep 14 '11 at 13:44
  • It does, but the strangest point is that this code always works fine! – superM Sep 14 '11 at 13:47
  • are you trying to do this on the onCreate of an activity? – blessanm86 Sep 22 '11 at 00:13
  • is the width of your textView set to wrap_content? – superM Sep 22 '11 at 08:21
  • @118218 - http://stackoverflow.com/questions/6939002/if-i-call-getmeasuredwidth-or-getwidth-for-layout-in-onresume-they-return-0 – Ziem May 06 '14 at 08:53
0

Before get measured width or height call below method :

anyview.measure(0,0);

without calling this method you all wage getting 0 value for height and width

Narendra Baratam
  • 828
  • 1
  • 12
  • 26
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43