0

In my application I would like to be able to allow the user to search the contents of a scrollable TextView with a fair bit of content in it (essentially an article). Much like the "search in page" functionality on a web browser, whereby the user enters a search term and each time they click search, they jump to the next match for that term.

The first element of that implementation, the highlighting of the search term within the article, is easy - just set a span with a background where the term is found. What I'm struggling with is how to scroll to each of these matches when the user presses "search". I've looked everywhere but can't seem to find anybody else who has tried this (but am happy to be proved wrong!) so would appreciate any guidance.

So, in summary: Given a search term, I want to scroll to each match within a TextView for that term.

Chris Ward
  • 101
  • 7

1 Answers1

0

Right, I managed to find a better way of doing it. Instead of using a TextView, use an EditText with editing disabled. You will know the index of the match, so:

myEditText.setSelection(startIndex, startIndex + searchTermLength);

This will automatically scroll you down to the search term if it is not visible.

Chris Ward
  • 101
  • 7