java - Clickable words inside of TextView Android -
this question has answer here:
i have textview contains hashtags ex. #first #second #third
. question how can detect hashtag clicked can perform action - eg. make toast of word. possible using textview widget? should use other widget istead?
update
i found solution using this example. hope others in future!
you can use spannable string achieve this:
spannablestring ss = new spannablestring("your string"); string[] words = ss.split(" "); for(final string word : words){ if(word.startswith("#")){ clickablespan clickablespan = new clickablespan() { @override public void onclick(view textview) { //use word here make decision } }; ss.setspan(clickablespan, ss.indexof(word), ss.indexof(word) + word.length(), spanned.span_exclusive_exclusive); } } textview textview = (textview) findviewbyid(r.id.hello); textview.settext(ss); textview.setmovementmethod(linkmovementmethod.getinstance());
Comments
Post a Comment