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.
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.
Styling Standard Links in XML and Code
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.
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.
Use setLinkTextColor only for link styling. setTextColor changes the non-link text and will not override the link color.
Making HTML Links Clickable and Readable
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.
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.
Customizing Individual Links With ClickableSpan
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.
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
textColorLinkorsetLinkTextColorwhen the wholeTextViewshares one link color. - Add
LinkMovementMethodwhenever links should be tappable. - Use
ClickableSpanfor 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
- How to change color of the back arrow in the new material theme?
- How to change color of the back arrow in the new material theme?
- How to change colors of a Drawable in Android?
- How to change display name of an app in react-native
- How to change font of UIButton with Swift
- How to change font of UIButton with Swift
- How to change fontFamily of TextView in Android
- How to change fontFamily of TextView in Android
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.