My app need such scenario like in a textView I stored "@dev will goto home and @Roy will go to Station. I want to open a dialer activity to call the clicked person.
But using this code if I click on any @words it shows whole string. am toasted string.
User can enter his own string and may be he/she enter or not the @contact and there is not defined index of the same.
multiAutoCompleteTextView.setText(addClickablePart(desc), TextView.BufferType.SPANNABLE);
private SpannableString addClickablePart(String str) {
SpannableString ss = new SpannableString(str);
Pattern pattern = Pattern.compile("[@]\\w*");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
// do toasting
TextView b = (TextView)textView;
String buttonText = b.getText().toString();
Toast.makeText(ViewNote.this,buttonText,Toast.LENGTH_SHORT).show();
}
};
ss.setSpan(clickableSpan, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return ss;
}