Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make several clickable parts of text in TextView

How can I make several clickable parts of text in TextView. Every clickable part must have his own action.

like image 483
degratnik Avatar asked Oct 07 '13 14:10

degratnik


People also ask

Can I make a TextView clickable?

Just like Buttons and ImageViews we can add onClickListeners to TextViews by simply adding the attribute android:onClick="myMethod" to your TextView XML tag. The other way, TextView tv = (TextView) this.


1 Answers

you can use android.text.style.ClickableSpan

    SpannableString ss = new SpannableString("Hello World");     ClickableSpan span1 = new ClickableSpan() {         @Override         public void onClick(View textView) {             // do some thing         }     };      ClickableSpan span2 = new ClickableSpan() {         @Override         public void onClick(View textView) {             // do another thing         }     };      ss.setSpan(span1, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     ss.setSpan(span2, 6, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);      textView.setText(ss);     textView.setMovementMethod(LinkMovementMethod.getInstance()); 
like image 88
stinepike Avatar answered Sep 28 '22 16:09

stinepike