Highlight searched text in ListView items

匿名 (未验证) 提交于 2019-12-03 01:14:02

问题:

I have a ListView and i am using a custom adapter to show data. Now i want to change searched text letter colour as in above screen shot.

Here is the code for SearchView

@Override public boolean onCreateOptionsMenu(Menu menu) {     MenuInflater inflater = getMenuInflater();     inflater.inflate(R.menu.actionbar_menu_item, menu);     SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);     final SearchView searchView = (SearchView) menu.findItem(R.id.action_search)             .getActionView();     searchView.setSearchableInfo(searchManager             .getSearchableInfo(getComponentName()));     searchView.setOnQueryTextListener(this);         return super.onCreateOptionsMenu(menu);         }  public boolean onQueryTextChange(String newText) {     // this is adapter that will be filtered       if (TextUtils.isEmpty(newText)){             lvCustomList.clearTextFilter();       }       else{             lvCustomList.setFilterText(newText.toString());        }     return false;  }  @Override public boolean onQueryTextSubmit(String query) {     return false; } 

Thank you.

回答1:

I assume that you have a custom Adapter with getCount() and getView() implemented and already filtering items, and you just need the bold part.

To achieve that, you need to use a SpannableString, which is basically text with markup attached. For example, a TextAppearanceSpan can be used to change typeface, font style, size, and color.

So, you should update your adapter's getView() to change the part where you use textView.setText() into something more or less like this:

String filter = ...; String itemValue = ...;  int startPos = itemValue.toLowerCase(Locale.US).indexOf(filter.toLowerCase(Locale.US)); int endPos = startPos + filter.length();  if (startPos != -1) // This should always be true, just a sanity check {     Spannable spannable = new SpannableString(itemValue);     ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE });     TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);      spannable.setSpan(highlightSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);     textView.setText(spannable); } else     textView.setText(itemValue); 


回答2:

Android Search Highlight Example [Case Insensitive Order]

1. Simple Search (Html): [Specific Word]

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 + ")", "$1");             textView.setText(Html.fromHtml(fullText, Html.FROM_HTML_MODE_LEGACY), TextView.BufferType.SPANNABLE);         } else {             fullText = fullText.replaceAll("(?i)(" + searchText + ")", "$1");             textView.setText(Html.fromHtml(fullText), TextView.BufferType.SPANNABLE);         }     } catch (Exception e) {         textView.setText(fullText);     } } 

2. Simple Search (Spannable): [Specific Word]

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): [Full Word]

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 

4. Details Search (Advanced): [Full Word]

If you use any of this method your problem will be solved... IngShaaAllah.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!