I'm trying to implement a comment like list where you can mention other users in the comments, and then be able to click on that to go to their profile. For example, when posting a comment on reddit you can mention another user with /u/username.
The one problem i'm running into is how to make it so I can click on the text to load that user's profile.
For the comment list I essentially have a custom list of textviews. From googling around I saw that it is possible to be able to click on some text in a textview using a clickable span. However, I haven't been able to get this to work in a list of textviews. Can someone help?
This is the relevant sections of my code:
Clickable span I declared as a member variable:
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Intent intent = new Intent(context, ProfileActivity.class);
// ParseUser.getQuery().whereEqualTo(Constants.kQollegeUserPreferredUsernameKey, sta)
intent.putExtra("User", ParseUser.getCurrentUser().getObjectId());
context.startActivity(intent);
}
};
Inside getView:
TextView answer_text_view = answerView.getAnswerTextView();
String text = answer.getAnswerText();
if(text.contains("@")) {
text += " ";
final SpannableStringBuilder sb = new SpannableStringBuilder(text);
List<Integer> start = new ArrayList<>();
int index = text.indexOf("@");
while (index >= 0) {
start.add(index);
index = text.indexOf("@", index + 1);
}
for(int i = 0; i<start.size(); i++){
sb.setSpan(fcs, start.get(i), text.indexOf(" ", start.get(i)), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.setSpan(clickableSpan, start.get(i), text.indexOf(" ", start.get(i)), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
answer_text_view.setText(sb);
}