android - I can't get setSpan working -


i have string e.g: "@user liked photo! 2h ago" in thin typeface.

this string consists of 3 parts;

1: @user -> should typeface.normal , clickable

2: liked photo! -> stays same (thin , black colored)

3: 2h ago -> should colored gray.

spannable spannedtime = new spannablestring(time); spannable clickableusername = new spannablestring(username); clickableusername.setspan(new stylespan(typeface.normal), 0, clickableusername.length(), 0); // 1st part make normal typeface spannedtime.setspan(new backgroundcolorspan(color.gray), 0, spannedtime.length(), 0); // 3rd part make gray  clickableusername.setspan(new clickablespan() {     @override     public void onclick(view view) {         callprofileactivity();     } }, 0, clickableusername.length(), spanned.span_exclusive_inclusive);// 1st part make clickable  this.settext(clickableusername + " " + notificationbody + " " + spannedtime); 

but none of them has effects.

the java compiler doesn't know spannable. when do

this.settext(clickableusername + " " + notificationbody + " " + spannedtime); 

java creates string concatination spannablestring.

to create spannable string intent do, should use spannablestringbuilder.

spannablestringbuilder spannable = new spannablestringbuilder(); spannable.append(clickableusername, new stylespan(typeface.normal), 0); spannable.append(' ').append(notificationbody).append(' '); spannable.append(time, new backgroundcolorspan(color.gray), 0); spannable.setspan(new clickablespan() {     @override     public void onclick(view view) {         callprofileactivity();     } }, 0, username.length(), spanned.span_exclusive_inclusive); this.settext(spannable); 

Comments