Reputation: 1060
My code looks like this,
String content = "Hi this is @Naveen. I'll meet @Peter in the evening.. Would you like to join @Sam??";
TextView contentTextView=(TextView)userHeader.findViewById(R.id.contentTextView);
contentTextView.setText(content);
Before setting the text in the textview, I would like to add click event for @Naveen, @Peter and @Sam.. When the user taps on these texts I want to open a new intent.. Is that possible? Any pointers would be quite helpful.
Upvotes: 0
Views: 991
Reputation: 9730
You can try to use Linkify with custom patterns.
However, if that doesn't suit your needs you can try this:
SpannableString ss = new SpannableString("Hi this is @Naveen. I'll meet @Peter in the evening.. Would you like to join @Sam??");
ClickableSpan clickableSpanNaveen = new ClickableSpan() {
@Override
public void onClick(View textView) {
//Do Stuff for naveen
}
};
ClickableSpan clickableSpanPeter = new ClickableSpan() {
@Override
public void onClick(View textView) {
//Do Stuff for peter
}
};
ClickableSpan clickableSpanSam = new ClickableSpan() {
@Override
public void onClick(View textView) {
//Do Stuff for sam
}
};
ss.setSpan(clickableSpanNaveen, 11, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(clickableSpanPeter, 29, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(clickableSpanSam, 76, 79, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView contentTextView=(TextView)userHeader.findViewById(R.id.contentTextView);
contentTextView.setText(ss);
contentTextView.setMovementMethod(LinkMovementMethod.getInstance());
Upvotes: 4
Reputation: 40203
You can use the Linkify
class, which provides methods for adding links to TextViews
. You can use the addLinks(TextView textView, Pattern pattern, String scheme)
method, which needs you to specify the TextView
, a Pattern
for the text you want to match and a custom scheme that will be used to match Activities
that can work with this kind of data. The Activity
that you want to be opened when clicking on links must declare this scheme in its intent-filter
. Hope this helps.
Upvotes: 3