Making Part of TextView Clickable

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

  1. textview.setOnClickListener(new View.OnClickListener()  
  2. {  
  3.     @Override  
  4.     public void onClick(View v)   
  5.     {  
  6.   
  7.     }  
  8. });  
But what if I want the listener for only some text? For example, “This is demo Android program,” now I want the click listener just for the word “Android.” Yes, this is possible by using ClickableSpan.

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

Syntax
  1. ClickableSpan clickableSpan = new ClickableSpan()  
  2. {  
  3.     @Override  
  4.     public void onClick(View textView)  
  5.     {  
  6.   
  7.     }  
  8.     @Override  
  9.     public void updateDrawState(TextPaint ds)  
  10.     {  
  11.         super.updateDrawState(ds);  
  12.     }  
  13. };  
Now you need to attach this object with your SpannableString.
  1. SpannableString ss =new SpannableString("This is demo android program");  
  2. ss.setSpan(clickableSpan,13,19,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
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 importantly, set setMovementMethod() on TextView.

  1. textView.setText(ss);  
  2. textView.setMovementMethod(LinkMovementMethod.getInstance()); 
Here you are done, whenever you click on ”Android,” the onClick() method of ClickableSpan will be called.

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

activity_main.xml
  1. <RelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  4. android:layout_height="match_parent"  
  5. android:padding="20dp">  
  6.     <TextView android:id="@+id/txtData"  
  7. android:layout_width="wrap_content"  
  8. android:textAppearance="?android:attr/textAppearanceLarge"  
  9. android:layout_height="wrap_content" />  
  10. </RelativeLayout>  
MainActivity.java
  1. public class MainActivity extends Activity   
  2. {  
  3.   
  4.     private TextView txtData;  
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState)  
  7.     {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.   
  11.         txtData = (TextView) findViewById(R.id.txtData);  
  12.         String text = "I love to do programming in @Android @IOS @JAVA";  
  13.   
  14.         SpannableString spanString = new SpannableString(text);  
  15.         Matcher matcher = Pattern.compile("@([A-Za-z0-9_-]+)").matcher(spanString);  
  16.   
  17.         while (matcher.find())  
  18.         {  
  19.             spanString.setSpan(new ForegroundColorSpan(Color.parseColor("#0000FF")), matcher.start(), matcher.end(), 0); //to highlight word havgin '@'  
  20.             final String tag = matcher.group(0);  
  21.             ClickableSpan clickableSpan = new ClickableSpan()  
  22.             {  
  23.                 @Override  
  24.                 public void onClick(View textView)  
  25.                 {  
  26.                     Log.e("click""click " + tag);  
  27.                     String searchText = tag.replace("@"""); //replace '@' with blank character to search on google.  
  28.                     Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.co.in/search?q=" + searchText));  
  29.                     startActivity(browserIntent);  
  30.                 }  
  31.                 @Override  
  32.                 public void updateDrawState(TextPaint ds)  
  33.                 {  
  34.                     super.updateDrawState(ds);  
  35.   
  36.                 }  
  37.             };  
  38.             spanString.setSpan(clickableSpan, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  39.         }  
  40.   
  41.         txtData.setText(spanString);  
  42.         txtData.setMovementMethod(LinkMovementMethod.getInstance());  
  43.     }  
  44. }