Remove Underline from Links in Textview - Android

Remove underline from links in TextView - Android

You can do it in code by finding and replacing the URLSpan instances with versions that don't underline. After you call Linkify.addLinks(), call the function stripUnderlines() pasted below on each of your TextViews:

    private void stripUnderlines(TextView textView) {
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);
s.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
s.setSpan(span, start, end, 0);
}
textView.setText(s);
}

This requires a customized version of URLSpan which doesn't enable the TextPaint's "underline" property:

    private class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}

How to remove underline from the hyperlink textview in xamarin android


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;
}
}

How to Remove Underline from hyperlink's text in android ?

after trying a lot finally i found the solution.
I removed the link from string.xml
and added this code in my java file.

Button btnGoogle = ( Button GoogleAlert.findViewById(
R.id.btnGoogle );
btnGoogle.setMovementMethod(
LinkMovementMethod.getInstance() );
btnGoogle.setOnClickListener( new OnClickListener()
{
@Override
public void onClick( View v )
{
Uri uri = Uri.parse(
"http://www.google.com" );
Intent intent = new Intent( Intent.ACTION_VIEW, uri );
startActivity( intent );
buyAlert.dismiss();
}
} );

and it worked perfectly.

how to remove underline for textview in android

You can try

 textview.setPaintFlags(textview.getPaintFlags() & (~ Paint.UNDERLINE_TEXT_FLAG));

or more broadly just set,

textview.setPaintFlags(0) but the first option is more exact

How to remove underline in hyperlink

As you mentioned you have to add spanable
first add this function(this is a kotlin extention function for textview):

fun TextView.removeLinksUnderline() {
val spannable = SpannableString(text)
for (u in spannable.getSpans(0, spannable.length, URLSpan::class.java)) {
spannable.setSpan(object : URLSpan(u.url) {
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
}
}, spannable.getSpanStart(u), spannable.getSpanEnd(u), 0)
}
text = spannable

}

then to use this function and remove underline type:

replace your textview with correct one(name of your textview)

txtView.removeLinksUnderline()   

more info about kotlin extention functions: this

more about removing hyperlinks: this

Remove underline from TextView which uses Widget.EditText style

Try to set transparent background in your textview

android:background="@android:color/transparent"

LIKE

    <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hm_bal"
android:textColor="@color/colorPrimaryDark"
android:textSize="14sp"
android:textStyle="bold"
android:layout_gravity="left"
android:padding="3dp"
android:gravity="top"
android:text="----"
android:background="@android:color/transparent"
style="@android:style/Widget.EditText" />


Related Topics



Leave a reply



Submit