Part of TextView clickable
source link: https://www.ravirupareliya.com/blog/part-of-textview-clickable/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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
- object of ClickableSpan.
- start position of text
- 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.
activity_main.xml
MainActivity.java
Output
Ravi Rupareliya
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK