I have code to search a TextView
and return the searched word with a highlight. The problem is that it keeps the previously searched words highlighted too, instead of starting highlighting anew. Following is my code:
String ett = et.getText().toString();
String tvt = tvdisplay.getText().toString();
int ofe = tvt.indexOf(ett,0);
Spannable WordtoSpan = new SpannableString(tvdisplay.getText());
for (int ofs=0; ofs<tvt.length() && ofe != -1; ofs=ofe+1)
{
ofe = tvt.indexOf(ett, ofs);
if (ofe == -1)
break;
else
{
WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00),ofe,ofe+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvdisplay.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
}
}
}
});
}