0

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"
Community
  • 1
  • 1
David Corsalini
  • 7,958
  • 8
  • 41
  • 66
  • Question 2 is a possible duplicate of http://stackoverflow.com/questions/8753163/how-can-i-perform-case-insensitive-pattern-search-and-case-preserving-replacemen – ajb Oct 22 '13 at 22:14
  • You're right, it works, could find it before. Should I provide the answer in a new Answer, edit on the question or just shut up? – David Corsalini Oct 22 '13 at 22:23
  • I don't think we need to say anything more. – ajb Oct 23 '13 at 00:45

2 Answers2

1

You have to assign the result of the fullString.replace(textToHighlight, htmlString); method to a value, if you want to use it. return fullString.replace(textToHighlight, htmlString); should solve the first problem.

Akkusativobjekt
  • 2,005
  • 1
  • 21
  • 26
1

String objects are immutable. That means that there is no way to change the content of a String object once it has been initialized.

The replace() method does not change the String object you call it on; instead, it returns a new String object with the replacement that you wanted to do.

You're not doing anything with the return value of the replace() method, so your code has no effect. You're just returning the original string. Change your code to return the result of the replace() method:

return fullString.replace(textToHighlight, htmlString);

To solve your other problem, you'll need to make your method more sophisticated. You could do that by using replaceFirst() instead of replace(), which takes a regular expression as the first argument (instead of a plain string). You'll have to build the regular expression carefully from textToHighlight and your requirements.

Jesper
  • 202,709
  • 46
  • 318
  • 350