Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android loop through string to color specific words

Tags:

string

android

I have a string that has specific words I want to color. Those words are the one's that start with #.

    if(title.contains("#")){

        SpannableString WordtoSpan = new SpannableString(title);   

        int idx = title.indexOf("#");
        if (idx >= 0) {

            Pattern pattern = Pattern.compile("[ ,\\.\\n]");
            Matcher matcher = pattern.matcher(title);
            int wordEnd = matcher.find(idx)? matcher.start() : -1;

            if (wordEnd < 0) {
                wordEnd = title.length();
            }
            WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE),
                        idx,
                        wordEnd,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }           

        holder.txtTitle.setText(WordtoSpan);
    } else {
        holder.txtTitle.setText(title);
    }

Now lets take for example this string Bold part is for example the colored words.

I love cheese #burgers, and with #ketchup.

^ This is my current thing with my code. What I want is to color every # word, not just first one.

ex. It'll be like

I love cheese #burgers, and with #ketchup.

I think I need to loop? But couldn't get it clearly and working x.x


Update: Latest try. List becomes blank.

    SpannableString WordtoSpan = new SpannableString(title);

    int end = 0;

    Pattern pattern = Pattern.compile("[ ,\\.\\n]");
    Matcher matcher = pattern.matcher(title);

    int i = title.indexOf("#");

    for (i = 0; i < title.length(); i++) {
    if (i >= 0) {

        int wordEnd = matcher.find(i)? matcher.start() : -1;

        if (wordEnd < 0) {
            wordEnd = title.length();
        }
        WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE),
                    i,
                    wordEnd,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        i = end;
    }    

    holder.txtTitle.setText(WordtoSpan);
    }
like image 330
John Jared Avatar asked Dec 12 '22 09:12

John Jared


2 Answers

I had provided this answer earlier here - https://stackoverflow.com/a/20179879/3025732

String text = "I love chicken #burger , cuz they are #delicious";
//get the text from TextView

Pattern HASHTAG_PATTERN = Pattern.compile("#(\\w+|\\W+)");
Matcher mat = HASHTAG_PATTERN.matcher(text);

while (mat.find()) {
  String tag = mat.group(0);

  //String tag will contain the hashtag
  //do operations with the hashtag (change color)
}

[EDIT]

I've modified the code and done it according to what you need:

SpannableString hashText = new SpannableString(text.getText().toString());
Matcher matcher = Pattern.compile("#([A-Za-z0-9_-]+)").matcher(hashText);
while (matcher.find())
{
     hashText.setSpan(new ForegroundColorSpan(Color.BLUE), matcher.start(), matcher.end(), 0);
}
text.setText(hashText);

Here's a screenshot of it:

enter image description here

like image 180
Joel Fernandes Avatar answered Apr 08 '23 13:04

Joel Fernandes


I'm surprised that no one has mentioned this:

// String you want to perform on
String toChange = "I love cheese #burgers, and with #ketchup.";

// Matches all characters, numbers, underscores and dashes followed by '#'
// Does not match '#' followed by space or any other non word characters
toChange = toChange.replaceAll("(#[A-Za-z0-9_-]+)", 
                         "<font color='#0000ff'>" + "$0" +  "</font>");

// Encloses the matched characters with html font tags

// Html#fromHtml(String) returns a Spanned object
holder.txtTitle.setText(Html.fromHtml(toChange));

#0000ff ==>> Color.BLUE

Screenshot:

enter image description here

like image 37
Vikram Avatar answered Apr 08 '23 13:04

Vikram