Clickable SpannableString in android -


i have bellow code:

spannablestring ss = new spannablestring("translators"); clickablespan myclickablespan = new clickablespan() {     @override     public void onclick(view v) {     // something;     } }; ss.setspan(myclickablespan, 0, ss.length(), spanned.span_exclusive_exclusive); 

but when add spannablestring string unclickable , not underline

string thanks="also "+ss; 

any ideas?

this line problem:

string thanks="also "+ss; 

the right-hand side of + expression spannable string, left hand side regular string (with no spanning support). + operator on strings causes java compiler put .tostring() after ss variable (in null-safe way), , converting spannable string normal string discards spans.

to fix it, rid of string concatenation. use spannablestringbuilder in place of spannablestring, , use insert(...) method put "also the" in there without losing spans.

there's simpler way though, question code came quite close:

spannablestring ss = new spannablestring("also translators"); clickablespan myclickablespan = new clickablespan() { /*...*/ };  int spanstart = 20; // length of "also " ss.setspan(myclickablespan, spanstart, ss.length(), spanned.span_exclusive_exclusive); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -