0

I want to give background color to the text that I receive from a List. At the moment I can get highlighted all the words, but I would like to exclude the spaces between the words

Initially I tried was to use a Pattern Matcher Regex solution so to exclude the double spaces, I was appending to the List. Then I realised that this solution was not the best because I could not exclude the spaces. So I decided to use a SpannableStringBuilder and append the items inside a for loop. But does not work I get only the first word highlighted and not all the single words( separated by a blank(not coloured)word

val spannable = SpannableStringBuilder()
            val span = BackgroundColorSpan(yellowColor))


            listOfUsers.forEach {
                val string = it.users

                spannable.append(string)
                spannable.setSpan(span, 0, string.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            }

            textValue.text = spannable

I expect to see not only the first word, but the single words updated. Please notice that I am aware that the problem is in setSpan where I put as start 0 but I do no know how to make start working so to highlight the right initial string[I] character

Ivano
  • 25
  • 5

2 Answers2

0

You can create method that returns Spannable like below:

public Spanned formatAutoSuggestText(final String autoSuggestText) {
        if (autoSuggestText == null) {
            return Html.fromHtml("");
        }

        try {
            String modifiedAutoSuggestText= "" ;
            final String searchText = "Text to highlight";
            final Pattern pattern = Pattern.compile(StringUtils.INSENSITIVE_CASE + searchText);
            final Matcher matcher = pattern.matcher(autoSuggestText);  
            int end = 0;
            while (matcher.find()) {
                final String subStringMatchFound = autoSuggestText.substring(end, matcher.end());
                final String stringToBeReplaced = autoSuggestText.substring(matcher.start(), matcher.end());
                final String stringToReplace = "<b><font color='" + mContext.getResources().getColor(R.color.search_autosuggest_highlighted_text) + "'>" +matcher.group()+ "</font></b>";
                modifiedAutoSuggestText += subStringMatchFound.replace(stringToBeReplaced,stringToReplace);
                end = matcher.end();
            }
            modifiedAutoSuggestText += autoSuggestText.substring(end);
            return Html.fromHtml(modifiedAutoSuggestText);
        }
        catch (final Exception e){
            return Html.fromHtml(autoSuggestText);
        }

    }

The code is in Java. you can change this based on your needs.

  • this does not help me unfortunately. the problem is in "Pattern.compile" I need to exclude the spaces(double spaces), while compile gives a matcher.find() instruction that does not work for my case, I need to EXCLUDE the double space, not search some text – Ivano Jun 07 '19 at 11:12
  • You can use below link to exclude the 2 or more spaces. [Java how to replace 2 or more spaces with single space in string and delete leading and trailing spaces](https://stackoverflow.com/questions/2932392/java-how-to-replace-2-or-more-spaces-with-single-space-in-string-and-delete-lead) – Aditya Singh Jun 07 '19 at 12:03
  • I do not need to exclude the spaces, I added them on purpose so that words came separated from my List – Ivano Jun 07 '19 at 12:57
0

You can write regex like below to match multiple spaces. You can replace as well.

fun regexTest(){
    val words = listOf("Hello", "Hello World1", "Hello  World2",
        "Hello    World3", "Hello World4   ")

    val pattern = "\\s+".toRegex()


    words.forEach { word ->
        if (pattern.containsMatchIn(word)) {
            println("match ---> $word")
            var temWord = word.replace("\\s+"," ")

        }
    }

    var result:ArrayList<String> = ArrayList()
    for (s in words) {
        result.add(s.replace(pattern, " "))
    }

    for(wordToPrint in result){
        println("replaced ---> $wordToPrint")
    }

}

Below is the output for the above program:

match ---> Hello World1
match ---> Hello  World2
match ---> Hello    World3
match ---> Hello World4   
replaced ---> Hello
replaced ---> Hello World1
replaced ---> Hello World2
replaced ---> Hello World3
replaced ---> Hello World4 

You can modify the regex according to your needs.

Rajiv Ranjan
  • 333
  • 4
  • 13
  • I do not need to replace spaces, and the problem as I said cames from the setSpan line when I have always 0 as start. But thank you to help – Ivano Jun 07 '19 at 13:02
  • How are you finding the stringlength. can you put some code. And put your input and expected output for better understanding. – Rajiv Ranjan Jun 07 '19 at 13:15
  • ops there was a typo I wrote now string.length, so is not stringlenght. This is how I am looking for the string length of the single instances of the List that come as `it`. and this links show you how I am expecting the text of the array to be highlighted https://github.com/splitwise/TokenAutoComplete – Ivano Jun 07 '19 at 13:27