Search code examples
javaandroidhighlight

how to highlight search result in editText, without losing break lines


i hope you are fine.

after trying many codes, just one worked 100% for me, but the result appear without break lines, this is the code (taken from here) :

String textToHighlight = edittext2.getText().toString();
String oldString = edittext1.getText().toString();
String newString = oldString.replaceAll(textToHighlight, "<span 
style='background-color: RED'>"+textToHighlight+"</span>");
textview1.setText(Html.fromHtml(newString));

and this is screenshot for the oldString :

before

and this is screenshot of restlt(newString):

after

i want to get result with highlight searched words without losing break lines of original text. i hope you help and thanks in advance


Solution

  • add <br> HTML tag in the end after your's </span> closing tag