android how to highlight text in TextView

android how to highlight text in TextView
‮efer‬r to:lautturi.com
public void setHighLightedText(TextView textView, String textToHighlight) {
    String tvt = textView.getText().toString();
    int ofe = tvt.indexOf(textToHighlight, 0);
    Spannable wordToSpan = new SpannableString(textView.getText());
    for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
        ofe = tvt.indexOf(textToHighlight, ofs);
        if (ofe == -1)
            break;
        else {
            wordToSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + textToHighlight.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(wordToSpan, TextView.BufferType.SPANNABLE);
        }
    }
}
Created Time:2017-09-12 07:25:13  Author:lautturi