Skip to content
Advertisement

How to remove underline in hyperlink

I have a TextView on my ‘settings’ activity:

    <TextView
        android:id="@+id/review"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#FFFFFF"
        android:drawablePadding="15dp"
        android:gravity="center_vertical"
        android:text="@string/review"
        android:textColor="#595959"
        android:textColorLink="#595959"
        android:textSize="12sp"
        android:textStyle="bold"
        app:drawableLeftCompat="@drawable/ic_review"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.055"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view"
        app:layout_constraintVertical_bias="0.876" />

I have defined my URL on the string.

<string name="review"><a href="https://play.google.com/store">Submit a review</a></string>

On my settings.kt page I have the following code onCreate:

val mTextView = findViewById<TextView>(R.id.review)

mTextView.movementMethod = LinkMovementMethod.getInstance()

From what I have found, I need to add a ‘spannable’ in relation to the above (settings.kt) but I’m not sure how to apply it, as everyone adds hyperlinks differently.

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement