Last active
August 29, 2015 14:07
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ... | |
| 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