Android
TextView
Hyperlink
Text Color
Android Development

how to change color of textview hyperlink?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Changing link color in an Android TextView depends on how the link is created. Auto-linked URLs, HTML links, and custom spans all render as links, but they are not styled through exactly the same path. The correct solution is usually simple once you separate whole-view link styling from per-link styling.

If the TextView contains links produced by android:autoLink or HTML parsing, the built-in link color is controlled by textColorLink. This is the best choice when every link in the view should use the same color.

xml
1<TextView
2    android:id="@+id/helpText"
3    android:layout_width="wrap_content"
4    android:layout_height="wrap_content"
5    android:autoLink="web"
6    android:linksClickable="true"
7    android:text="Read https://developer.android.com"
8    android:textColor="@android:color/black"
9    android:textColorLink="@color/link_blue" />
xml
<color name="link_blue">#1565C0</color>

You can also set the link color at runtime. That is useful when the color comes from theme state, a remote configuration, or a brand system shared across several screens.

kotlin
1import android.graphics.Color
2import android.text.method.LinkMovementMethod
3import android.text.util.Linkify
4import android.widget.TextView
5
6fun configureAutoLink(textView: TextView) {
7    textView.text = "Visit https://example.com for support"
8    Linkify.addLinks(textView, Linkify.WEB_URLS)
9    textView.movementMethod = LinkMovementMethod.getInstance()
10    textView.setLinkTextColor(Color.parseColor("#0D47A1"))
11}

Use setLinkTextColor only for link styling. setTextColor changes the non-link text and will not override the link color.

A common source of confusion is HTML text. Developers often parse HTML into a Spanned, see the link style appear, and assume the link is ready. It is not clickable until the TextView has a movement method.

kotlin
1import android.os.Build
2import android.text.Html
3import android.text.method.LinkMovementMethod
4import android.widget.TextView
5
6fun bindHtmlLink(textView: TextView) {
7    val html = "Open the <a href=\"https://developer.android.com\">Android docs</a>."
8
9    textView.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
10        Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY)
11    } else {
12        @Suppress("DEPRECATION")
13        Html.fromHtml(html)
14    }
15
16    textView.movementMethod = LinkMovementMethod.getInstance()
17    textView.setLinkTextColor(0xFF00897B.toInt())
18}

This pattern works well for legal text, help screens, or localized strings that include embedded anchors. If your design requires different colors per theme, keep the hex value out of the function and read a color resource instead.

textColorLink affects every link in the view. When one phrase should look different, use a ClickableSpan. This lets you control the color, underline behavior, and click action of a specific range.

kotlin
1import android.graphics.Color
2import android.text.SpannableString
3import android.text.Spanned
4import android.text.TextPaint
5import android.text.method.LinkMovementMethod
6import android.text.style.ClickableSpan
7import android.view.View
8import android.widget.TextView
9
10fun bindCustomLink(textView: TextView, onTermsClick: () -> Unit) {
11    val text = "By continuing, you agree to the Terms of Service"
12    val start = text.indexOf("Terms")
13    val end = text.length
14    val spannable = SpannableString(text)
15
16    val span = object : ClickableSpan() {
17        override fun onClick(widget: View) {
18            onTermsClick()
19        }
20
21        override fun updateDrawState(ds: TextPaint) {
22            ds.color = Color.parseColor("#C62828")
23            ds.isUnderlineText = false
24        }
25    }
26
27    spannable.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
28    textView.text = spannable
29    textView.movementMethod = LinkMovementMethod.getInstance()
30    textView.highlightColor = Color.TRANSPARENT
31}

This approach is better than globally recoloring the whole TextView when only one substring is interactive. It also keeps the rest of the text aligned with the normal typography of the screen.

Choosing the Right Approach

Use XML or setLinkTextColor when all links in a TextView share one style. Use ClickableSpan when a single phrase needs custom behavior or a different visual treatment. Use HTML only when your content source is already HTML or localization makes inline anchors easier to manage.

Also think about accessibility. A link color should still have enough contrast against the background. Removing underlines can be acceptable, but only if the text remains obviously interactive through color, weight, or surrounding context.

If the same link color appears throughout the app, move it into your theme or shared color resources. Repeating literal color values in multiple fragments or activities makes future design changes harder than they need to be.

Common Pitfalls

The most common mistake is changing textColor and expecting the link to change with it. Android treats link color separately, so the right property is textColorLink or setLinkTextColor.

Another issue is forgetting LinkMovementMethod. In that case the link may look correct but tapping it does nothing.

A third mistake is using HTML for a single custom link when a span would be simpler and safer. HTML is convenient for rich text content, but it is not the best tool for every interactive phrase.

Finally, teams often pick a link color that looks good on a white layout and then reuse it on cards, dialogs, or dark surfaces where it becomes unreadable. Check contrast in the real screen, not just in isolation.

Summary

  • Use textColorLink or setLinkTextColor when the whole TextView shares one link color.
  • Add LinkMovementMethod whenever links should be tappable.
  • Use ClickableSpan for per-link color and behavior control.
  • Prefer theme or resource colors over repeated literal values.
  • Verify contrast and interaction cues so links remain obvious and accessible.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.