Clickable text
TextView
Android development
User interface
Programming tips

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.

Browse interview questions

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:

kotlin
1import android.graphics.Color
2import android.os.Bundle
3import android.text.SpannableString
4import android.text.Spanned
5import android.text.TextPaint
6import android.text.method.LinkMovementMethod
7import android.text.style.ClickableSpan
8import android.view.View
9import android.widget.TextView
10import androidx.appcompat.app.AppCompatActivity
11
12class MainActivity : AppCompatActivity() {
13    override fun onCreate(savedInstanceState: Bundle?) {
14        super.onCreate(savedInstanceState)
15
16        val textView = TextView(this)
17        val text = "By continuing, you agree to the Terms of Service."
18        val spannable = SpannableString(text)
19
20        val target = "Terms of Service"
21        val start = text.indexOf(target)
22        val end = start + target.length
23
24        val span = object : ClickableSpan() {
25            override fun onClick(widget: View) {
26                println("Terms clicked")
27            }
28
29            override fun updateDrawState(ds: TextPaint) {
30                super.updateDrawState(ds)
31                ds.color = Color.BLUE
32                ds.isUnderlineText = true
33            }
34        }
35
36        spannable.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
37        textView.text = spannable
38        textView.movementMethod = LinkMovementMethod.getInstance()
39        textView.highlightColor = Color.TRANSPARENT
40
41        setContentView(textView)
42    }
43}

Only the selected character range is clickable. The rest of the TextView behaves normally.

Why LinkMovementMethod Is Required

This line is easy to forget:

kotlin
textView.movementMethod = LinkMovementMethod.getInstance()

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.

kotlin
1val text = "Read the Terms and Privacy Policy."
2val spannable = SpannableString(text)
3
4fun makeSpan(action: () -> Unit) = object : ClickableSpan() {
5    override fun onClick(widget: View) = action()
6}
7
8val termsStart = text.indexOf("Terms")
9val privacyStart = text.indexOf("Privacy Policy")
10
11spannable.setSpan(
12    makeSpan { println("Terms clicked") },
13    termsStart,
14    termsStart + "Terms".length,
15    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
16)
17
18spannable.setSpan(
19    makeSpan { println("Privacy clicked") },
20    privacyStart,
21    privacyStart + "Privacy Policy".length,
22    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
23)

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.

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 SpannableString and ClickableSpan to make only part of a TextView clickable.
  • Always enable LinkMovementMethod so 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
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.