Set textColorLink to textAllCaps?

I have a text view that should be in all caches and mark any URLs found in it with a specific color, so naturally I tried textColorLink , with the textAllCaps = "true" option , however the url is not colored, I assume that the regex does not match the upper urls, since the url is colored if the same text is in lowercase.

I tried to solve it with this:

Spannable formatted = new SpannableString(text);
Pattern url = Pattern.compile(
            "(https?)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
Matcher matcher = url.matcher(text.toLowerCase());

while (matcher.find())
{
    Log.e("TEST",matcher.group());
    int begIndex = matcher.start();
    int endIdx = begIndex + matcher.group().length() - 1;
    Log.e("Found", String.valueOf(begIndex));
    formatted.setSpan(new ForegroundColorSpan(
                    getResources().getColor(android.R.color.holo_red_light)),
                begIndex, endIdx, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
mTextView.setText(formatted);

      

It appears to find text, however once again it is not colored. I've been in this for hours, how do you solve this?

+3


source to share


1 answer


when trying to upperCase the string will lose color, but if you add another SpannableString and pass that string.toUpperCase then you can set the SpannableString ...

SpannableString formatted = new SpannableString(urlString);
Pattern url = Pattern.compile("(https?)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
Matcher matcher = url.matcher(urlString.toLowerCase());

//Here you save the string in upper case
SpannableString stringUpperCase = new SpannableString(formatted.toString().toUpperCase());

while (matcher.find()) {

   int begIndex = matcher.start();
   int endIdx = begIndex + matcher.group().length() - 1;
   stringUpperCase.setSpan(new ForegroundColorSpan(R.color.Red),
                        0, formatted.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
TextView text = (TextView) findViewById(R.id.textView);
text.setText(string);

      

Must work...




Remove text from xmlAllCaps = "true"

+2


source







All Articles