Part of TextView clickable

Today we will discuss about how can we set click listener for some part of your TextView.

As you all know we can set click listener on textview like we use to set in other controls(Button, ImageView etc).

But what if i want listener for some text only. For example “This is demo android program”, now i want click listener just for “android” word. Yes this is possible with using ClickableSpan.

If object of ClickableSpan is attached with TextView with a movement method of LinkmovementMethod, text click will call onClick(View) method.

Syntax :

Now you need to attach this object with your SpannableString.

Attributes of setSpan

  1. object of ClickableSpan.
  2. start position of text
  3. end position of text

Now you need to set that text in your TextView and most important to set setMovementMethod() on TextView.

Here you are done, whenever you click on”android”, onClick() method of ClickableSpan will be called.

Let’s see one example program for more than one click listener on TextView.



Part of TextView clickable

Follow me

Ravi Rupareliya

He is author of Android Gig. He loves to explore new technologies and have worked on Android, React Native, Action on Google and Flutter.
Ravi Rupareliya
Follow me
YouTube Integration with Android Studio
Inbox Style Notification Like Whatsapp