Advanced Search Highlight Example [Case Insensitive Order]
1. Simple search (Html):
public static void setSearchTextHighlightSimpleHtml(TextView textView, String fullText, String searchText) { searchText = searchText.replace("'", ""); try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { fullText = fullText.replaceAll("(?i)(" + searchText + ")", "<span style=\"background-color:#FCFF48;\"><b><big><font color='#a10901'>$1</font></big></b></span>"); textView.setText(Html.fromHtml(fullText, Html.FROM_HTML_MODE_LEGACY), TextView.BufferType.SPANNABLE); } else { fullText = fullText.replaceAll("(?i)(" + searchText + ")", "<b><big><font color='red'>$1</font></big></b>"); textView.setText(Html.fromHtml(fullText), TextView.BufferType.SPANNABLE); } } catch (Exception e) { textView.setText(fullText); } }
2. Simple search (Spannable):
public static void setSearchTextHighlightSimpleSpannable(TextView textView, String fullText, String searchText) { searchText = searchText.replace("'", ""); // highlight search text if (null != searchText && !searchText.isEmpty()) { SpannableStringBuilder wordSpan = new SpannableStringBuilder(fullText); Pattern p = Pattern.compile(searchText, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(fullText); while (m.find()) { int wordStart = m.start(); int wordEnd = m.end(); // Now highlight based on the word boundaries ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{0xffa10901}); TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null); wordSpan.setSpan(highlightSpan, wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); wordSpan.setSpan(new BackgroundColorSpan(0xFFFCFF48), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); wordSpan.setSpan(new RelativeSizeSpan(1.25f), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } textView.setText(wordSpan, TextView.BufferType.SPANNABLE); } else { textView.setText(fullText); } }
3. Quick search (advanced):
public static void setAdvancedTitleHighlight(TextView textView, String fullText, String searchText) { searchText = searchText.replace("'", ""); final String WORD_SINGLE = " "; // highlight search text if (null != searchText && !searchText.isEmpty() && !searchText.equals(WORD_SINGLE)) { SpannableStringBuilder wordSpan = new SpannableStringBuilder(fullText); Pattern p = Pattern.compile(searchText, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(fullText); while (m.find()) { final char WORD_BOUNDARY = ' '; int wordStart = m.start(); while (wordStart >= 0 && fullText.charAt(wordStart) != WORD_BOUNDARY) { --wordStart; } wordStart = wordStart + 1; int wordEnd = m.end(); while (wordEnd < fullText.length() && fullText.charAt(wordEnd) != WORD_BOUNDARY) { ++wordEnd; } // Now highlight based on the word boundaries ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{0xffa10901}); TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null); wordSpan.setSpan(highlightSpan, wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); wordSpan.setSpan(new BackgroundColorSpan(0xFFFCFF48), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); wordSpan.setSpan(new RelativeSizeSpan(1.25f), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } textView.setText(wordSpan, TextView.BufferType.SPANNABLE); } else { textView.setText(fullText); } }
4. Detailed search (advanced):
public static void setAdvancedDetailsHighlight(TextView textView, String fullText, String searchText) { searchText = searchText.replace("'", ""); final String WORD_SINGLE = " "; final String WORD_SINGLE1 = "\n"; final String WORD_SINGLE2 = "("; final String WORD_SINGLE3 = ")"; final String WORD_SINGLE4 = "।"; final String WORD_SINGLE5 = "."; final String WORD_SINGLE6 = ","; final String WORD_SINGLE7 = ";"; final String WORD_SINGLE8 = "?"; final String WORD_SINGLE9 = "-"; final String WORD_SINGLE10 = "+"; // highlight search text if (null != searchText && !searchText.isEmpty() && !searchText.equals(WORD_SINGLE) && !searchText.equals(WORD_SINGLE1) && !searchText.equals(WORD_SINGLE2) && !searchText.equals(WORD_SINGLE3) && !searchText.equals(WORD_SINGLE4) && !searchText.equals(WORD_SINGLE5) && !searchText.equals(WORD_SINGLE6) && !searchText.equals(WORD_SINGLE7) && !searchText.equals(WORD_SINGLE8) && !searchText.equals(WORD_SINGLE9) && !searchText.equals(WORD_SINGLE10)) { SpannableStringBuilder wordSpan = new SpannableStringBuilder(fullText); Pattern p = Pattern.compile(searchText, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(fullText); while (m.find()) { final char WORD_BOUNDARY = ' '; final char WORD_BOUNDARY1 = '\n'; final char WORD_BOUNDARY2 = '('; final char WORD_BOUNDARY3 = ')'; final char WORD_BOUNDARY4 = '।'; final char WORD_BOUNDARY5 = '.'; final char WORD_BOUNDARY6 = ','; final char WORD_BOUNDARY7 = ';'; final char WORD_BOUNDARY8 = '?'; final char WORD_BOUNDARY9 = '-'; int wordStart = m.start(); while (wordStart >= 0 && fullText.charAt(wordStart) != WORD_BOUNDARY && fullText.charAt(wordStart) != WORD_BOUNDARY1 && fullText.charAt(wordStart) != WORD_BOUNDARY2 && fullText.charAt(wordStart) != WORD_BOUNDARY3 && fullText.charAt(wordStart) != WORD_BOUNDARY4 && fullText.charAt(wordStart) != WORD_BOUNDARY5 && fullText.charAt(wordStart) != WORD_BOUNDARY6 && fullText.charAt(wordStart) != WORD_BOUNDARY7 && fullText.charAt(wordStart) != WORD_BOUNDARY8 && fullText.charAt(wordStart) != WORD_BOUNDARY9) { --wordStart; } wordStart = wordStart + 1; int wordEnd = m.end(); while (wordEnd < fullText.length() && fullText.charAt(wordEnd) != WORD_BOUNDARY && fullText.charAt(wordEnd) != WORD_BOUNDARY1 && fullText.charAt(wordEnd) != WORD_BOUNDARY2 && fullText.charAt(wordEnd) != WORD_BOUNDARY3 && fullText.charAt(wordEnd) != WORD_BOUNDARY4 && fullText.charAt(wordEnd) != WORD_BOUNDARY5 && fullText.charAt(wordEnd) != WORD_BOUNDARY6 && fullText.charAt(wordEnd) != WORD_BOUNDARY7 && fullText.charAt(wordEnd) != WORD_BOUNDARY8 && fullText.charAt(wordEnd) != WORD_BOUNDARY9) { ++wordEnd; } // Now highlight based on the word boundaries ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{0xffa10901}); TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null); wordSpan.setSpan(highlightSpan, wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); wordSpan.setSpan(new BackgroundColorSpan(0xFFFCFF48), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); wordSpan.setSpan(new RelativeSizeSpan(1.25f), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } textView.setText(wordSpan, TextView.BufferType.SPANNABLE); } else { textView.setText(fullText); } }