Reputation: 75
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#
How can i remove the underline from the hyperlink?
Upvotes: 5
Views: 2769
Reputation: 18186
York's answer won't work if you have a mix of text and urls in your TextView content, it sets the entire first line of text in your textview into a UrlSpan. Instead, to preserve where the urls should actually go, you can do the following:
public static void StripUnderlinesFromLinks (this TextView textView) {
var spannable = new SpannableStringBuilder (textView.TextFormatted);
var spans = spannable.GetSpans (0, spannable.Length (), Java.Lang.Class.FromType (typeof (URLSpan)));
foreach (URLSpan span in spans) {
var start = spannable.GetSpanStart (span);
var end = spannable.GetSpanEnd (span);
spannable.RemoveSpan(span);
var newSpan = new URLSpanNoUnderline (span.URL);
spannable.SetSpan(newSpan, start, end, 0);
}
textView.TextFormatted = spannable;
}
class URLSpanNoUnderline : URLSpan {
public URLSpanNoUnderline (string url) : base (url) {
}
public override void UpdateDrawState (TextPaint ds) {
base.UpdateDrawState (ds);
ds.UnderlineText = false;
}
}
Upvotes: 0
Reputation: 87
just update the method as below
the order should be : setting text color, calling base mehtod , setting underline mode
public override void UpdateDrawState(TextPaint ds)
{
ds.LinkColor = Android.Graphics.Color.Blue.ToArgb();
base.UpdateDrawState(ds);
ds.UnderlineText = false;
}
Upvotes: 0
Reputation: 9084
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);
}
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;
}
}
Upvotes: 2