1

I have a viewPager in my app. I want to put a long block of text in the viewPager. I don't want the text to be scrollable, I want it to be separated or parted to pages, and when the user pages it shows the rest of text in the other pages.

How can I approach this?

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

0

You can instantiate a number of Fragments based on the dimension of the text, measured via public float measureText (String text). Then you determine the screen as stated in Get screen dimensions in pixels

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

And this way you can calculate the number of fragments you need.
For the splitting part the quickest solution I can think of would be a loop with measureText (String text, int start, int end) to determine where the text has to be split. (There might be more elegant solutions to this)

Community
  • 1
  • 1
super-qua
  • 3,148
  • 1
  • 23
  • 30
0

Android use (Boring|Static|Dynamic)Layout classes to measure and wrap text, Theses class take width as constructor param and will calc the total height of you text, they also provide methods like getLineHeight(int line) to get line info, so you can calculate String offset according to these info. I create a class to do this, see my answer here: https://stackoverflow.com/a/30468884/2800351

Community
  • 1
  • 1
Cryse Hillmes
  • 196
  • 1
  • 3