I'm trying to remove link underlines from textView in a Xamarin projects. The following snippet works well in a java project:
textView.setText("You must agree to our terms and conditions");
Pattern pattern1 = Pattern.compile("terms and conditions");
Linkify.addLinks(textView, pattern1, "http://www.someLink.com", null, new Linkify.TransformFilter() {
@Override
public String transformUrl(Matcher match, String url) {
return "";
}
});
Spannable s = new SpannableString(textView.getText());
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span: spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
NoUnderline noUnderline = new NoUnderline();
s.setSpan(noUnderline, start, end, 0);
}
textView.setText(s);
Now, translating the exact same code in C#, I came to the following:
TextView.Text = "You must agree to our terms and conditions";
Java.Util.Regex.Pattern termsAndConditionsMatcher = Java.Util.Regex.Pattern.Compile("terms and conditions");
Linkify.AddLinks(TextView, termsAndConditionsMatcher, "https://www.someLink.com/", null, new EmptyLinkTransformer());
var spannable = new SpannableString(TextView.Text);
var spans = spannable.GetSpans(0, spannable.Length(), Java.Lang.Class.FromType(typeof(URLSpan)));
var urlSpans = new URLSpan[spans.Length];
for (var i = 0; i < urlSpans.Length; i++)
{
urlSpans[i] = spans[i] as URLSpan;
}
foreach (URLSpan span in urlSpans)
{
int start = spannable.GetSpanStart(span);
int end = spannable.GetSpanEnd(span);
var updated = new NoUnderlineSpan();
spannable.SetSpan(updated, start, end, 0);
}
TextView.TextFormatted = spannable;
The problem is that spannable.GetSpans(0, spannable.Length(), Java.Lang.Class.FromType(typeof(URLSpan))) returns 0 elements, even though I see the links being colored and underlined on the UI.
This and this post suggest that I'm using the method correctly. Am I doing something wrong here?