4

I am trying to build a book reader. And now I am having a long text which fills up the entire screen and even some of the text is out of screen. I want to know how to find the last letter that is fully visible in screen so that I can split text into pages.

For example, all lines before

"Bad news, Vernon," she said......

are fully visible text, and remaining text has to be moved to another page.

enter image description here

Like this(another page of text starts with "Bad news")

enter image description here

Here is the layout that I used in above example

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="...some text..." />
</RelativeLayout>
Aravind A R
  • 2,674
  • 1
  • 15
  • 25
Season
  • 1,178
  • 2
  • 22
  • 42
  • 1
    you are trying to achieve page splinting please check this out https://stackoverflow.com/questions/20204348/how-to-break-styled-text-into-pages-in-android – argando Jun 20 '17 at 07:24
  • I have tried the sample app provided from argando's link and it seems working as what I want – Season Jun 20 '17 at 07:39

1 Answers1

1

I think that predicting where a text is going to end would be difficult/impossible unless you were using a monospaced font, e.g. Courier. But instead of taking this approach, an alternative I can suggest is to simply deliver a constant number of characters for each page of your book reader. Yes, this means that with a non monospaced font you won't know the exact number of lines to be rendered. However, the number of lines taken up should fall within a fairly tight range, and most likely would not be perceptible to the user.

You can use a ScrollView here, which would contain the actual TextView holding the text for each page. Here is a skeleton of what that might look like:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="...some text..." />
    </ScrollView>
</RelativeLayout>

Using a ScrollView, the user can then simply drag up and down to catch any content which might not fit into a single screen. Again, this frees you from having to worry about delivering an exact amount of content.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I know using ScrollView with TextView is an easy approach for display long text but still it is not a page-splitting approach – Season Jun 20 '17 at 07:28
  • @Season What I'm saying is to use a scroll view and then you don't need to worry about "page-splitting." Most readers probably follow this approach. – Tim Biegeleisen Jun 20 '17 at 07:28