1

I have a .txt file inside my assets folder that has a massive HTML in it...

I call it to a string, remove some HTML tags using JSoup Library, and set this string as a TextView's text:

InputStream is;
TextView tv = (TextView)findViewById(R.id.tv);
String html;

try {
    is = getAssets().open("html.txt");
    html = convertStreamToString(is);
} catch (Exception e) {
    e.printStackTrace();
}

Document doc = Jsoup.parse(html);

Elements links = doc.select("a[href]");
for (Element link : links) {
    doc.select("a").unwrap();
}

doc.select("title").remove();

tv.setText(fromHtml(html));

It results in a TextView showing a really big and beautiful formatted HTML code

When the user touches any word in that TextView, I want to change the word's background color, marking it, and storing that mark to be marked again when user restart the application... How could I achieve that?

EDIT:

Also, how could I work with Spannable and Html.fromHtml() together?

T. Lima
  • 238
  • 3
  • 13

1 Answers1

0
  1. This post can help you get the index of your word. OnClick Position in String in TextView. Save the start index of word.

  2. From the index to end of word, use spannablestring to highlight the word. How to search for a word in a string and highlight word in a text view in android?

  3. When you load the text again, you can do the same for the stored index of word.

Community
  • 1
  • 1
Zahid Rasheed
  • 1,536
  • 1
  • 10
  • 28
  • Using that examples, I would have to use `Spannable.setSpan()` before setting the string as a TextView's text, right? But to `setSpan()`, I already have to know the index of the clicked word, don't I? How could I make the string, and then, use `setSpan()` to every word of the string? – T. Lima Mar 13 '17 at 02:52