Skip to content

Instantly share code, notes, and snippets.

@billmote
Last active August 29, 2015 14:07
Show Gist options
  • Save billmote/b88f277999aa733ce6f7 to your computer and use it in GitHub Desktop.
Save billmote/b88f277999aa733ce6f7 to your computer and use it in GitHub Desktop.
A SpannableStringBuilder used, in this case, to bold parts of a result while typing in a search field.
// ...
final String contactName = this.contact.getFullname();
final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(contactName);
final StyleSpan boldSpan = new StyleSpan(android.graphics.Typeface.BOLD);
if (!TextUtils.isEmpty(searchTerm) && contactName.toLowerCase().contains(searchTerm.toLowerCase())) {
int startIndex = contactName.toLowerCase().indexOf(searchTerm.toLowerCase());
int endIndex = startIndex + searchTerm.length();
spannableStringBuilder.setSpan(boldSpan, startIndex, endIndex, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
this.fullname.setText(spannableStringBuilder);
} else {
this.fullname.setText(contactName);
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment