1

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

I call it to a string and set this string as a TextView's text:

InputStream is;
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);

    tv = (TextView)findViewById(R.id.tv);

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

}

public static String convertStreamToString(InputStream is) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line).append("\n");
    }
    reader.close();
    return sb.toString();
}

What I want to achieve is:

  • The TextView shows the text like it would show in a browser (I'm using Html.fromHtml to achieve this).

  • When a user touches a word from that TextView, the word's background color changes (place a mark on this word and the mark will last even if the application is restarted).

  • Give the user an option to mark just the word, or the paragraph the word is in.

For the paragraph marking, I thought I could split the string into paragraphs, make a TextView for each paragraph, and set a click listener on it, to change the whole TextView's color...

For the words, I couldn't find any way to achieve that, except to split the HTML string into words and make a TextView for each word (the whole HTML has about 4000 words). And about storing the mark, I don't know either.

Could anyone help me please?

jacefarm
  • 6,747
  • 6
  • 36
  • 46
T. Lima
  • 238
  • 3
  • 13
  • I think you might want to search for `clickablespan`!, see [this](http://stackoverflow.com/questions/10696986/how-to-set-the-part-of-the-text-view-is-clickable). I hope it helps you – Atef Hares Mar 09 '17 at 02:55
  • You probably should've tried to focus on just one main objective instead of the 3 listed. Becuase if people only know 1 of the 3, then I see them unlikely to answer. You should possibly consider refactoring this if you don't get much response within the next couple of days. – Zze Mar 09 '17 at 03:01
  • @AtefHares I will take a look and try to adapt that to my needs, thank you! – T. Lima Mar 09 '17 at 03:05
  • @Zze Ok, if I get no answer I will refactor my question... Thank you! – T. Lima Mar 09 '17 at 03:05

0 Answers0