Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove underline from the hyperlink textview in xamarin android

Explanation

I have used textview inside that there is a link, when i click on that it goes to the webpage. To create a link i used anchor tag of html.

This is the xml file where i created the link

<TextView
        android:text="@string/Home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="fill_horizontal"
        android:layout_weight="0.5"
        android:clickable="true"
        android:textColorLink="#b3b3b3"
        android:textSize="15dp"
        android:id="@+id/Terms" />

This is the string.xml file where i created the link

<string name="Home"><a href="https://www.google.co.in/ " style="color:rgb(179, 179, 179);text-decoration:none">Home</a>

I tried using the css style text-decoration:none which doesn't work.

Also i tried from this link Remove underline from links in TextView - Android In that spannable doesn't work in c#

Question

How can i remove the underline from the hyperlink?

This is the image showing the hyperlink with underline

like image 411
Anish Avatar asked Oct 29 '22 20:10

Anish


1 Answers

How to remove underline from the hyperlink textview in xamarin android

The link you posted above is correct. I simplify Reuben Scratton's code and was able to remove the underline.

URLSpanNoUnderline class :

public class URLSpanNoUnderline : URLSpan
{
    public URLSpanNoUnderline(String url) : base(url)
    {
    }

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

Remove the underline from the hyperlink TextView :

private void stripUnderlines(TextView textView)
{
    SpannableString s = new SpannableString(textView.Text);
    s.SetSpan(new URLSpanNoUnderline(textView.Text), 0, s.Length(), SpanTypes.ExclusiveExclusive);
    textView.SetText(s, TextView.BufferType.Spannable);
}

Update :

Here is a workaround to solve the click issue:

You could use ClickableSpan instead of URLSpan to implement the same feature. You could refer to my answer, then modify your code like this :

textview.MovementMethod = LinkMovementMethod.Instance;
textview.Text = GetString(Resource.String.Home);

SpannableString ss = new SpannableString(textview.Text);
ss.SetSpan(new MyClickableSpan(this), 0, ss.Length(), SpanTypes.ExclusiveExclusive);
textview.SetText(ss, TextView.BufferType.Spannable);

Here is the MyClickableSpan code :

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("https://www.google.co.in/"));
        mainActivity.StartActivity(browserIntent);
    }

    public override void UpdateDrawState(TextPaint ds)
    {
        base.UpdateDrawState(ds);
        ds.Color = Color.Red;
        ds.UnderlineText = false;
    }
}
like image 153
York Shen Avatar answered Nov 01 '22 08:11

York Shen