9

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 , the filtered result should show the searched word highlighted. like:

enter image description here

I am using this SearchView widget as :

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.myMenu , menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView sv = new SearchView(getActivity());

    // Changing the color of Searchview widget text field to white.     
    int searchSrcTextId = getResources().getIdentifier("android:id/search_src_text", null, null);  
    EditText searchEditText = (EditText) sv.findViewById(searchSrcTextId);  
    searchEditText.setTextColor(Color.WHITE); 

    sv.setOnQueryTextListener(this);
    searchItem.setActionView(sv);
    super.onCreateOptionsMenu(menu, inflater);
}
Ali Ansari
  • 219
  • 3
  • 14
  • the search result is listview u have to change the text color of listview items – Maveňツ Aug 05 '14 at 11:17
  • http://stackoverflow.com/questions/20604893/change-android-textview-color-from-a-string try this it will help you – Pandiyan Muthu Aug 05 '14 at 11:18
  • If i chnage the color of list item, it will change the whole line. M just wanted to change the searched letter/letters. – Ali Ansari Aug 05 '14 at 11:19
  • Please check this answer: http://stackoverflow.com/a/23967561/82788 I believe it's exactly what you need. – matiash Aug 07 '14 at 02:32
  • possible duplicate of [Highlight searched text in ListView items](http://stackoverflow.com/questions/23905028/highlight-searched-text-in-listview-items) – matiash Aug 07 '14 at 10:46

3 Answers3

8

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.

John smith
  • 1,781
  • 17
  • 27
  • what does int spanStart and int spanEnd do? It creates a new index integer for the search text? How does length() method help here? – AJW Feb 10 '17 at 19:39
  • @influx have you try notifyDatasetchanged() after my code executed? – John smith Jun 14 '17 at 05:57
5

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();
    }

enter image description here

Nilesh
  • 1,013
  • 14
  • 21
2

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);
                }
            }
        }
    });
Eshack Nazir
  • 354
  • 3
  • 16