As you all know, we can set click listener on textview like we use to set it in other controls (Button, ImageView etc).
- textview.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- }
- });
If the object of ClickableSpan is attached with TextView with a movement method of LinkmovementMethod, the text click will call onClick(View) method.
Syntax
- ClickableSpan clickableSpan = new ClickableSpan()
- {
- @Override
- public void onClick(View textView)
- {
- }
- @Override
- public void updateDrawState(TextPaint ds)
- {
- super.updateDrawState(ds);
- }
- };
- SpannableString ss =new SpannableString("This is demo android program");
- ss.setSpan(clickableSpan,13,19,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- object of ClickableSpan.
- start position of text
- end position of text
Now you need to set that text in your TextView and most importantly, set setMovementMethod() on TextView.
- textView.setText(ss);
- textView.setMovementMethod(LinkMovementMethod.getInstance());
Let’s see one example program for more than one click listener on TextView.
activity_main.xml
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="20dp">
- <TextView android:id="@+id/txtData"
- android:layout_width="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:layout_height="wrap_content" />
- </RelativeLayout>

Join the conversation! Your thoughts help the community grow.