-1

I have a TextView and contains the below text

The -[[community]]- is here to help you with -[[specific]]- coding, -[[algorithm]]-, or -[[language]]- problems.

I want anything inside -[[]]- take red color, How can I do that using Spannable?

And I don't want to show -[[ and ]]- in TextView

RaBaKa 78
  • 1,115
  • 7
  • 11
Taha Sami
  • 1,565
  • 1
  • 16
  • 43
  • https://stackoverflow.com/questions/3282940/set-color-of-textview-span-in-android – tamtom Nov 14 '21 at 10:32
  • Does this answer your question? [Set color of TextView span in Android](https://stackoverflow.com/questions/3282940/set-color-of-textview-span-in-android) – Noah Nov 14 '21 at 10:35
  • @Noah No that is different. – Taha Sami Nov 14 '21 at 10:40
  • It's the same. Read the whole post. – Noah Nov 14 '21 at 10:42
  • if the square brackets are necessary, you should the index of the String in the bracket and apply the colours using Spannables. Then you remove the brackets. – Noah Nov 14 '21 at 10:45

3 Answers3

0

You can use the CodeView library to highlight many patterns with different colors, in your case for example the code will be like this

CodeView codeView = findViewById(R.id.codeview);
codeView.addSyntaxPattern(Pattern.compile("-\\[\\[[a-zA-Z]+]]-"), Color.GREEN);
codeView.setTextHighlighted(text);

And the result will be:

enter image description here

If the highlighted keywords are unique you can highlight them without using -[[]]- just create a pattern that can cover them

You can change the color, add or remove patterns in the runtime

CodeView Repository URL: https://github.com/amrdeveloper/codeview

AmrDeveloper
  • 3,826
  • 1
  • 21
  • 30
  • This is just another option and developers choose what they want be depending on his case, – AmrDeveloper Nov 14 '21 at 10:51
  • well, am not against using the library, but using a library for this small task is just not good. – Noah Nov 14 '21 at 10:53
  • You are right Noah if is just one task, but this option is if you want to implement it many time or with multiple patterns and colors and if you want to change the highlight color depend on the current theme – AmrDeveloper Nov 14 '21 at 10:56
  • I don't like to use any external library for small things, Thanks for your answer. – Taha Sami Nov 14 '21 at 11:29
0

You can use SpannableStringBuilder and append parts of String colorizing them when necessary. For example,

static CharSequence colorize(
        String input, String open, String close, @ColorInt int color
) {
    SpannableStringBuilder builder = new SpannableStringBuilder();
    int openLen = open.length(), closeLen = close.length();
    int openAt, contentAt, closeAt, last = 0;
    while ((openAt = input.indexOf(open, last)) >= 0 &&
            (closeAt = input
                    .indexOf(close, contentAt = openAt + openLen)) >= 0) {
        int start = builder.append(input, last, openAt).length();
        int len = builder.append(input, contentAt, closeAt).length();
        builder.setSpan(
                new ForegroundColorSpan(color),
                start, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );
        last = closeAt + closeLen;
    }
    return builder.append(input, last, input.length());
}
Miha_x64
  • 5,973
  • 1
  • 41
  • 63
0

The value in the variable who named open must be different from the value in the variable who named close, If the value was the same will cause a problem. You need to change variables values only to work well.

String open = "-[[";
String close = "]]-";
int color = Color.RED;
String s1 = "The -[[community]]- is here to help you with -[[specific]]- coding, -[[algorithm]]-, or -[[language]]- problems.";
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(s1);
while (spannableStringBuilder.toString().contains(open) && spannableStringBuilder.toString().contains(close)) {
    spannableStringBuilder.setSpan(new ForegroundColorSpan(color), spannableStringBuilder.toString().indexOf(open) + open.length(), spannableStringBuilder.toString().indexOf(close) + close.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannableStringBuilder.replace(spannableStringBuilder.toString().indexOf(open), spannableStringBuilder.toString().indexOf(open) + open.length(), "").replace(spannableStringBuilder.toString().indexOf(close), spannableStringBuilder.toString().indexOf(close) + close.length(), "");
}
yourTextView.setText(spannableStringBuilder);
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Taha Sami
  • 1,565
  • 1
  • 16
  • 43