Splitting text into multiple text elements / snippets using ViewPager

I am trying to split the text into several fragments loaded into the ViewPager using the Android compatibility library. The idea is to have something similar to the Kindle application, where you view multiple pages of text.

Initially, my text source is a string defined in the strings.xml file, and I use an algorithm to determine the height of the device screen, and then appropriately break this string into several elements for the ViewPager. Since you cannot get the height of the TextView ahead of time, I just use a percentage to determine how to set the maximum size of the TextView on each page (this seems a little ugly).

Does this seem like a reasonable approach, or am I all wrong about this?

+7
source share
1 answer

Since you cannot get the height of the TextView ahead of time

Well, it depends on what you mean by "ahead of schedule" (time == text processing?).

  • You can measure() use ViewPager / TextView once and use your getMeasuredHeight()/getMeasuredWidth() in your calculations
  • You can wait for ViewTreeObserver.OnGlobalLayoutListener.onGlobalLayout() and use the "standard" layout dimensions for the same thing.
  • (not recommended) you can use ("screen height" - "decoration height") / "text height (font + spacing)" to find out how many text lines will fit into your TextView
  • Etc. and etc.
0
source

All Articles