How to remove underline from a hyperlink TextView control in Xmarine?

soumenhaldersoumenhalder Member ✭✭
edited October 2018 in Xamarin.Android

Hello,
I am using a simple TextView control in Xmarine project. I made it as hyperlink control from code behind by the below mentioned code,

string DATA = "https://forums.xamarin.com";
TextView.TextFormatted = Html.FromHtml("<a href=" + DATA + ">" + "ABCD" + "</a>");
TextView.MovementMethod = LinkMovementMethod.Instance;

But it makes the TextView as a link with an Underline.

I want to remove this underline. How it is possible?

Best Answer

Answers

  • soumenhaldersoumenhalder Member ✭✭
    edited October 2018

    @BillyLiu said:
    @soumenhalder

    I want to remove this underline. How it is possible?

    Yes. You need to customize URLSpan and override UpdateDrawState method.

    public class URLSpanNoUnderline : URLSpan
    {
        public URLSpanNoUnderline(string url) : base(url)
        {
        }
    
        public override void UpdateDrawState(TextPaint ds)
        {
            base.UpdateDrawState(ds);
            ds.UnderlineText = false;
        }
    }
    

    And set the text by:

            TextView textView = FindViewById<TextView>(Resource.Id.textView1);
            string DATA = "https://forums.xamarin.com";
            textView.TextFormatted = Html.FromHtml("<a href=" + DATA + ">" + "ABCD" + "</a>", FromHtmlOptions.ModeCompact);
            textView.MovementMethod = LinkMovementMethod.Instance;
            SpannableString s = new SpannableString(textView.Text);
            s.SetSpan(new URLSpanNoUnderline(textView.Text), 0, s.Length(), SpanTypes.ExclusiveExclusive);
            textView.SetText(s, TextView.BufferType.Spannable);
    

    Hello @BillyLiu,
    We are working on API Level 19. Here we are facing error java.lang.nosuchmethoderror for the below mentioned code
    textView.TextFormatted = Html.FromHtml("<a href=" + DATA + ">" + "ABCD" + "</a>", FromHtmlOptions.ModeCompact);

    Finally the app crashed. How can we resolve this for API Level 19?

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    @soumenhalder

    Please use Html.FromHtml(string) intead:

        textView.TextFormatted = Html.FromHtml("<a href=" + DATA + ">" + "ABCD" + "</a>" );
    
  • soumenhaldersoumenhalder Member ✭✭

    @BillyLiu said:
    @soumenhalder

    Please use Html.FromHtml(string) intead:

        textView.TextFormatted = Html.FromHtml("<a href=" + DATA + ">" + "ABCD" + "</a>" );
    

    Hello @BillyLiu,
    I have tried with the code that you have suggested, but it is giving another error Android.Content.ActivityNotFoundException:
    and finally app crashed. How can we resolve this.

    I have attached the application for better understanding. please check MainActivity.

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai
    edited October 2018

    @soumenhalder

    Sorry, my fault. Please replace textView.Text to DATA when call SetSpan().

    s.SetSpan(new URLSpanNoUnderline(DATA), 0, s.Length(), SpanTypes.ExclusiveExclusive);
    

  • soumenhaldersoumenhalder Member ✭✭

    @BillyLiu said:
    @soumenhalder

    Sorry, my fault. Please replace textView.Text to DATA when call SetSpan().

    s.SetSpan(new URLSpanNoUnderline(DATA), 0, s.Length(), SpanTypes.ExclusiveExclusive);
    

    Thank you @BillyLiu , Wonderful support!!! it's working...

Sign In or Register to comment.