1

Here Example : "some [string] with [the data i want] inside [ashish]"

i want to get string with blue color that are between "["blue colored"]" and replace [ (opening) (closing)] to just space. like facebook tagging statement.

bytecode77
  • 14,163
  • 30
  • 110
  • 141

1 Answers1

0

Try following method:

    private SpannableString makeBluish(String text)
{
    String temp = text;
    Pattern pattern = Pattern.compile("\\[([\\sa-zA-Z]+)\\]");
    Matcher matcher = pattern.matcher(text);
    while (matcher.find())
    {
        temp = temp.replace(matcher.group(0), (matcher.group(1)));
    }

    SpannableString linkedString = new SpannableString(temp);
    matcher = pattern.matcher(text);
    while (matcher.find())
    {

        final String token = matcher.group(1);

        int index = temp.indexOf(token);

        linkedString.setSpan(new ForegroundColorSpan(Color.BLUE), index, index + token.length(), SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
    }
    return linkedString;
}

And

textString.setText(makeBluish("some [string] with [the data i want] inside [ashish]"));

here textString is a TextView.

Sachin Chandil
  • 17,133
  • 8
  • 47
  • 65