Search code examples
androidtextviewhighlight

How to selected or highlight TextView programmatically in Android?


Currently, I am create a small app for reading. There is a textview to display text. We can bookmark text and store in a listview. I want to click from listview and it highlight or select the bookmark index in the textview. I can get the bookmark_index. Is there method like textview.setSelect(Start,Length)? or Any library can do it? Thank

Please look image view to understand my idea.

enter image description here


Solution

  • Use Spannable

    Spannable WordtoSpan = new SpannableString("partial colored text");        
    WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), startIndex, stopIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(WordtoSpan);