I need to highlight a TextView in a ListView in Android and looking at CommonsWare response here I've decided to use the .setText(Html.fromHtml(htmlString) method. So in my adapter I have this formatString() method.
I got 2 problems:
1) It doesn't seem to work.
2) Even if it would work, it would be limited, because the fullString can be all UpperCase, all LowerCase or with a Upper Case char as the first char in a word.
The textToHighlight will only be at the beginning of words in fullString.
private String formatString(String textToHighlight, String fullString) {
if (textToHighlight== null || textToHighlight.equals("")) {
return fullString;
}
String colore = "\"#0099CC\"";
String htmlSubString = "<font color=" + colore + ">"
+ textToHighlight+ "!</font>";
fullString.replace(textToHighlight, htmlString);
return fullString;
}
Output should be:
a) formatString("ca", "Cake Maccaroni") = "<font color=\"#0099CC\">Ca</font>ke Maccaroni"
b) formatString("ca", "CAKE MACCARONI") = "<font color=\"#0099CC\">CA</font>KE MACCARONI"
c) formatString("ca", "Maccaroni Cake") = "Maccaroni <font color=\"#0099CC\">Ca</font>ke"
d) formatString("ca", "MACCARONI CAKE") = "MACCARONI <font color=\"#0099CC\">CA</font>KE"
e) formatString("ca", "cake maccaroni") = "<font color=\"#0099CC\">ca</font>ke maccaroni"