Season
Season

Reputation: 1218

Android: how to determine last fully visible letter in TextView?

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>

Upvotes: 4

Views: 400

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522741

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.

Upvotes: 1

Related Questions