1

In my android application, I am displaying a long string into multiple pages. I achieve this by breaking the long string into a string array where every array element holds the number of characters which can fit on one screen (without scrolling). Also by using a previous/next button at the bottom of my activity I change the content of my textview to switch between pages(array elements).

Where I am stuck is in finding out how many characters will show on one page/screen. Remember I don’t want user to scroll. I use the paint.breaktext with screenwidth as the parameter and find out how many characters go in one line and then multiply it by number of lines on one screen to get the number of characters in a page. Android’s text wrapping at the end of each line is what gets my calculation of finding characters in a page, wrong. I am trying to put my own logic to accommodate for text wrapping but that is not working.

So my question is:

  • Is there any way I can find out that how many characters will show in one screen?
  • Or what can also help me is if I find out what logic is followed by android for wrapping the text for newline. Because if I know that, then I can readjust my number of characters on a page accordingly.
  • Or other option could be to disable text wrapping (not sure if possible). Because then I can use my own logic to wrap the text rather than trying to figure out Android’s text wrapping logic and adjusting my character-on-a-page accordingly.

Any help is much apreciated.

Cristian Holdunu
  • 1,626
  • 1
  • 15
  • 37
user1938357
  • 1,446
  • 3
  • 20
  • 32
  • Have you considered not using a TextView and just drawing text to a canvas on a custom view? TextView is always going to insert padding and other space, if you really want direct control over how it shows up you're better off doing it yourself. – Gabe Sechan Feb 09 '13 at 23:29
  • Also, to turn off wrapping use android:singleLine=true in the xml – Gabe Sechan Feb 09 '13 at 23:31
  • Thanks, Gabe. singleLine option looks interesting, as I can insert my own linebreaks then. Also Drawing text to canvas also is a good suggestion. Do you know any place i get some more help about that? – user1938357 Feb 10 '13 at 11:35
  • I don't know any tutorials. Check out the drawText command on the Canvas option, which draws text without wrapping, and the measure functions on the Paint object to see how big text will be. Then you'd need to overwrite the view's onDraw function to measure the text and draw it out 1 line at a time. – Gabe Sechan Feb 10 '13 at 16:39
  • I'm still in the process of researching this, but see [BreakIterator](https://developer.android.com/reference/java/text/BreakIterator.html). – Suragch Feb 14 '17 at 00:56
  • This [this answer](http://stackoverflow.com/a/42219474/3681880) for more about `BreakIterator` and [this answer](http://stackoverflow.com/a/41779935/3681880) for `StaticLayout`. – Suragch Feb 14 '17 at 08:45

0 Answers0