androidandroid-layouttexttextviewandroid-search

Android: Display concrete part of text in TextView


I'm creating search in app and highlighting part of text in TextView (with spannable). But text fields has fixed width/height and search match could be behind TextView border (not displayed to user).

Here is an example how TextView looks:

<TextView
    android:layout_width="256dp"
    android:maxLines="1"
    android:ellipsize="end"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="123456789 hello example of text field long line"
    android:textColor="?attr/clContent"
    android:textSize="24sp"/>

Example of single line TextView

If user searching for: "of text", how could I display that part in TextView? Expecting something like:

|... of text field long...|

The same problem with multiline TextView (e.g. if it has android:maxLines="3"), how to scroll to particular text part? Is it even possible?


I have an idea about how to do it manually. If single line TextView - remove first part of text (till needed word) and replace it with "...". For multiline the same, drop first part of text + replace with dots.

That idea looks okay, but maybe someone knows another way to achieve same result in less complex way?


Solution

  • The only thing that Textview should do is display.

    I think your idea is correct. Instead of find a way to allow Textview focus which part to show, the better way is produce proper data and show it simply.