How to click or tap on a TextView text
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Making text interactive is one of the most common requirements in Android development. Whether you need the entire TextView to respond to taps or only specific words within it to be clickable, Android provides several mechanisms to accomplish this. Understanding which approach to use and why saves you from fighting unexpected behavior around click feedback, focus, and text selection.
Making the Entire TextView Clickable
The simplest case is when the whole TextView should act like a button. You attach a click listener just as you would with any other View.
In Kotlin:
In Java:
If you want the TextView to show a ripple effect when tapped, add the android:clickable="true" and android:focusable="true" attributes in your XML, along with a selectable background:
Making Specific Words Clickable with ClickableSpan
Sometimes only a portion of the text should be tappable, for example a "Terms of Service" link inside a longer sentence. This is where ClickableSpan comes in. It lets you mark a range of characters as individually clickable.
The call to LinkMovementMethod.getInstance() is critical. Without it, Android does not route touch events to the spans inside the text. The highlightColor setting removes the default blue highlight rectangle that appears on tap, which often looks unpolished.
Auto-Linking with Linkify and android:autoLink
If your text contains URLs, phone numbers, or email addresses that should be clickable without manual span work, Android can detect and linkify them automatically.
In XML:
Programmatically in Kotlin:
The Linkify utility scans the text with pattern matchers and wraps detected items in URLSpan instances. It also sets the movement method for you. The android:autoLink attribute does the same thing declaratively.
Controlling Click Feedback and Highlight Color
By default, when a user taps a ClickableSpan, Android draws a translucent highlight over the text. You can customize or remove this:
For the entire-TextView-click scenario, the ?attr/selectableItemBackground drawable gives you a Material ripple. For span-level clicks, the highlight color is the primary visual feedback mechanism.
Common Pitfalls
- Forgetting
LinkMovementMethod: ClickableSpan silently does nothing without it, leading to frustrating debugging sessions. - Conflict between
setOnClickListenerandClickableSpan: If both are set, tapping a non-span area triggers the View click, but tapping a span triggers only the span. This inconsistency confuses users if not handled intentionally. - Not setting
highlightColorto transparent: The default blue highlight rectangle looks like a visual bug in most modern app designs. - Using
autoLinkwith manually set spans:autoLinkcan overwrite your customSpannableString, destroying your carefully placed ClickableSpans. Apply autoLink first or use Linkify programmatically with care. - Accessibility oversight: ClickableSpans should describe their action for screen readers. Override
updateDrawStateto visually distinguish links and ensure TalkBack can announce them.
Summary
- Use
setOnClickListenerwhen the entire TextView should respond to taps, and addselectableItemBackgroundfor ripple feedback. - Use
ClickableSpanwithLinkMovementMethodto make individual words or phrases tappable within a larger block of text. - Use
android:autoLinkorLinkifyto automatically detect and link URLs, emails, and phone numbers without manual span management. - Control highlight color via
textView.highlightColorto match your app's visual design. - Always test that
LinkMovementMethodis set when using spans, and be cautious about mixingautoLinkwith custom spannable text.

