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).
1 2 3 4 5 6 |
textview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); |
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 :
1 2 3 4 5 6 7 8 9 10 |
ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View textView) { } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); } }; |
Now you need to attach this object with your SpannableString.
1 2 |
SpannableString ss =new SpannableString("This is demo android program"); ss.setSpan(clickableSpan,13,19,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
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.
1 2 |
textView.setText(ss); textView.setMovementMethod(LinkMovementMethod.getInstance()); |
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
1 2 3 4 5 6 7 8 9 10 11 |
<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> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
public class MainActivity extends Activity { private TextView txtData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtData=(TextView)findViewById(R.id.txtData); String text="I love to do programming in @Android @IOS @JAVA"; SpannableString spanString = new SpannableString(text); Matcher matcher = Pattern.compile("@([A-Za-z0-9_-]+)").matcher(spanString); while (matcher.find()) { spanString.setSpan(new ForegroundColorSpan(Color.parseColor("#0000FF")), matcher.start(), matcher.end(), 0); //to highlight word havgin '@' final String tag = matcher.group(0); ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View textView) { Log.e("click", "click " + tag); String searchText=tag.replace("@",""); //replace '@' with blank character to search on google. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.co.in/search?q=" + searchText)); startActivity(browserIntent); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); } }; spanString.setSpan(clickableSpan, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } txtData.setText(spanString); txtData.setMovementMethod(LinkMovementMethod.getInstance()); } } |
Output
Ravi Rupareliya
Latest posts by Ravi Rupareliya (see all)
- Dialogflow Entities With Actions on Google - May 7, 2020
- Actions on Google Using Cloud Functions - February 3, 2020
- Create WhatsApp Stickers Android Application - April 19, 2019