Skip to content

Instantly share code, notes, and snippets.

@yujuwon
Created June 27, 2016 02:49
Show Gist options
  • Select an option

  • Save yujuwon/5ce78de28ea9c3436050898205536188 to your computer and use it in GitHub Desktop.

Select an option

Save yujuwon/5ce78de28ea9c3436050898205536188 to your computer and use it in GitHub Desktop.
private void setContent(){
String tag = "";
int i;
for(i = 0 ; i <mTagLists.size(); i++){
tag += "#" + mTagLists.get(i) + " ";
}
ArrayList<int[]> hashtagSpans = getSpans(tag, '#');
SpannableString tagsContent = new SpannableString(tag);
for(i = 0; i < hashtagSpans.size(); i++){
int[] span = hashtagSpans.get(i);
int hashTagStart = span[0];
int hashTagEnd = span[1];
Hashtag hashTag = new Hashtag(this);
hashTag.setOnClickEventListener(new Hashtag.ClickEventListener() {
@Override
public void onClickEvent(String data) {
}
});
tagsContent.setSpan(hashTag,
hashTagStart,
hashTagEnd,
0);
}
TextView tags_view = (TextView)findViewById(R.id.textview_tag);
if(tags_view != null) {
tags_view.setMovementMethod(LinkMovementMethod.getInstance());
tags_view.setText(tagsContent);
}
}
public ArrayList<int[]> getSpans(String body, char prefix) {
ArrayList<int[]> spans = new ArrayList<int[]>();
Pattern pattern = Pattern.compile(prefix + "\\w+");
Matcher matcher = pattern.matcher(body);
// Check all occurrences
while (matcher.find()) {
int[] currentSpan = new int[2];
currentSpan[0] = matcher.start();
currentSpan[1] = matcher.end();
spans.add(currentSpan);
}
return spans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment