How to set the part of the text view is clickable
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To make only part of an Android TextView clickable, use a span rather than a separate button or view. The standard approach is a SpannableString plus a ClickableSpan, with LinkMovementMethod enabled so touch events reach the clickable range.
Basic Clickable Span Setup
Here is the usual Kotlin pattern:
Only the selected character range is clickable. The rest of the TextView behaves normally.
Why LinkMovementMethod Is Required
This line is easy to forget:
Without it, the text may look like a link but taps will not be dispatched to the span.
That is why many implementations "almost work" visually but never react to touch.
Multiple Clickable Parts
You can attach several spans to one TextView.
This is useful for inline legal text, help links, and mixed navigation copy.
Styling and Text Changes
Avoid hard-coded numeric offsets when the displayed text may change because of:
- localization
- copy edits
- string formatting
Instead, compute the indexes from the final displayed text, as the earlier example did with indexOf(target).
That makes the code more robust when the string content changes later.
Accessibility and Touch Behavior
Clickable text should still look interactive. If you remove the underline or change the default link color, make sure the clickable portion remains visually distinct enough that users know it can be tapped.
Also remember that parent views can intercept touches. If taps never reach the span, inspect surrounding containers and gesture logic, not just the TextView.
When HTML Links Are Not the Best Fit
Android also supports HTML-style links in some text flows, but ClickableSpan is often easier to control in app code because you can define exact actions, styling, and multiple spans without relying on HTML parsing behavior.
For application-driven interaction, span-based control is usually the clearer approach.
Common Pitfalls
Forgetting LinkMovementMethod is the most common reason a clickable span does not respond.
Hard-coding character indexes makes the click range break as soon as the text changes or is translated.
Styling the clickable portion so it no longer looks interactive creates usability problems even if the code technically works.
Ignoring parent touch interception can make span taps seem broken when the real issue is higher up in the view hierarchy.
Summary
- Use
SpannableStringandClickableSpanto make only part of aTextViewclickable. - Always enable
LinkMovementMethodso touch events reach the span. - Compute span indexes from the real displayed text instead of hard-coding positions.
- Multiple clickable phrases can live in the same
TextView. - Keep the clickable text visually distinct and test touch behavior in the real layout.
Related reading
- How to set the text color of TextView in code?
- How to set the title of a Navigation Bar programmatically?
- How to set the title text color of UIButton?
- How to set the title text color of UIButton?
- How to set the width of a cell in a UITableView in grouped style
- How to set timeout in Retrofit library?
- How to set timeout in Retrofit library?
- How to set timer 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.