1

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?

2 Answers2

2

Xamarin: Unable to retrieve URLSpans from a TextView with links

Here is a workaround: You could use ClickableSpan instead of URLSpan to implement the same feature.

Custom a ClickableSpan class and override the UpdateDrawState to remove underline from the hyperlink TextView :

class MyClickableSpan : ClickableSpan
{
    private MainActivity mainActivity;

    public MyClickableSpan(MainActivity mainActivity)
    {
        this.mainActivity = mainActivity;
    }

    public override void OnClick(View widget)
    {
        Intent browserIntent = new Intent(Intent.ActionView, Uri.Parse("http://www.somelink.com/"));
        mainActivity.StartActivity(browserIntent);
    }

    public override void UpdateDrawState(TextPaint ds)
    {
        base.UpdateDrawState(ds);
        ds.Color = Color.Red;
        ds.UnderlineText = false;
    }
}

When use it :

TextView textview = FindViewById<TextView>(Resource.Id.Terms);

//You need add this line
textview.MovementMethod = LinkMovementMethod.Instance;

//This is the Text of TextView
SpannableString ss1 = new SpannableString("You must agree to our terms and conditions");

//22 means start position, ss1.Length() means end position
ss1.SetSpan(new MyClickableSpan(this), 22, ss1.Length(), SpanTypes.ExclusiveExclusive);

//set text for your TextView
textview.TextFormatted = ss1;
York Shen
  • 9,014
  • 1
  • 16
  • 40
0

You can get existing spans from a TextView with the following code

TextView mytextview = ....;

var spans = Android.Text.SpannableFactory.Instance.NewSpannable(mytextview.TextFormatted);

foreach (URLSpan span in spans.GetSpans(0, mytextview.Text.Length, Java.Lang.Class.FromType(typeof(URLSpan))))
{
    //Use an Android.Text.SpannableStringBuilder to build your new spannabletext
    var ssb = new Android.Text.SpannableStringBuilder();
    //Do your business here.
}

//Then set your text to your TextView
mytextview.SetText(ssb, TextView.BufferType.Spannable);

If you want to get the spans from the textview again, just do the same code again.

Pierre
  • 8,397
  • 4
  • 64
  • 80