How to highlight the filtered text while using SearchView widget in android

后端 未结 3 951
半阙折子戏
半阙折子戏 2021-02-15 08:00

I have implemented SearchView Widget in my app. Its working fine. Now i need to do is, whenever i type a word in my SearchView Bar

相关标签:
3条回答
  • 2021-02-15 08:10

    you can use Spannable TextView for this. hope so this Method will help you

    Method:

    public static CharSequence highlightText(String search, String originalText) {
        if (search != null && !search.equalsIgnoreCase("")) {
            String normalizedText = Normalizer.normalize(originalText, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "").toLowerCase();
            int start = normalizedText.indexOf(search);
            if (start < 0) {
                return originalText;
            } else {
                Spannable highlighted = new SpannableString(originalText);
                while (start >= 0) {
                    int spanStart = Math.min(start, originalText.length());
                    int spanEnd = Math.min(start + search.length(), originalText.length());
                    highlighted.setSpan(new ForegroundColorSpan(Color.BLUE), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    start = normalizedText.indexOf(search, spanEnd);
                }
                return highlighted;
            }
        }
        return originalText;
    }
    

    and return originalText will highlight text.

    0 讨论(0)
  • 2021-02-15 08:12

    You Should do this in onBindViewHolder() method (using RecyclerView)

    class YourAdapter

    String searchString="";
    
    @Override
        public void onBindViewHolder(AllMessageAdapter.DataObjectHolder holder, final int position) {
    
            holder.message.setText(mDataset.get(position).Message);
    
            AllMessageList.Message message=mDataset.get(position);
    
            String name = message.Message.toLowerCase(Locale.getDefault());
    
            if (name.contains(searchString)) {
    
                int startPos = name.indexOf(searchString);
                int endPos = startPos + searchString.length();
    
                Spannable spanString = Spannable.Factory.getInstance().newSpannable(holder.message.getText());
                spanString.setSpan(new ForegroundColorSpan(Color.RED), startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
                holder.message.setText(spanString);
            }
    
        }
    

    Your Filter(in Adapter)

    public void setFilter(ArrayList<AllMessageList.Message> countryModels,String searchString) {
            this.searchString=searchString;
            mDataset = new ArrayList<>();
            mDataset.addAll(countryModels);
            notifyDataSetChanged();
        }
    

    0 讨论(0)
  • 2021-02-15 08:24

    You can use this to highlight all the keywords.

     button.setOnClickListener(new OnClickListener() {      
          @Override
          public void onClick(View v) {
    
            String ett = edittext.getText().toString();
            String tvt = textview.getText().toString();
            int ofe = tvt.indexOf(ett, 0);
            Spannable spannable = new SpannableString(tvt);
    
            for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
            ofe = tvt.indexOf(ett, ofs);
    
                    if (ofe == -1)
                        break;
                    else {
                      ColorStateList blueColor = new ColorStateList(new nt[][] { new int[] {} }, new int[] { Color.BLUE });
                      TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
                      spannable.setSpan(highlightSpan, ofe, ofe+edittext.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                      textview.setText(spannable);
                    }
                }
            }
        });
    
    0 讨论(0)
提交回复
热议问题