skip to Main Content

I am struggling with this issue since 1 day. I want to apply custom font(in xml i am adding as fontFamily) to a textview. And also add apply underline for specific words in that textview with hyperlink and custom color.

I tried multiple ways like HTML text with Html.fromHTML(), Custom textview with Paint(), SpannableBuilder etc. Everytime either I can apply underline with clickable link and without custom font or I can apply only custom font but not underline(In some devices like Samsung, Motorola etc devices only).

2

Answers


  1. Chosen as BEST ANSWER

    Finally achieved my requirement using AwsomeTextHandler-Github library.


  2. I use SpannableStringBuilder to make part of the string Bold, Underline, Clickable, etc.

    val spannable = SpannableStringBuilder("My String to Underline")
    val span = StyleSpan(Typeface.UNDERLINE)
    // 14 is the start position and 22 is the end position
    spannable.setSpan(span, 14, 22,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
    

    To create a link.

    val clickableSpan: ClickableSpan = object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        // Do Something
                    }
                }
    spannable.setSpan(clickableSpan, <Start Position>, <End Position>, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
               
    // Change the color
    spannable.setSpan(ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorBlack)), <Start Position>, <End Position>, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
    // Make it Bold
    spannable.setSpan(StyleSpan(Typeface.BOLD), <Start Position>, <End Position>, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search