0

How to highlight search text result in RecyclerView. I found some posts regarding Spannable TextView, but not sure where to implement in my case. Appreciate you can look and assist.

Can any one help it is not highlight the search text

     public void onSearch(String query){
            Log.d("Query From Activity",query);
            ArrayList<TaskModel> filteredList = new ArrayList<>();
            //resultView.setVisibility(View.GONE);
            for (int i = 0; i < listTask.size(); i++) {

                final String jTitle = listTask.get(i).getAssigned().toLowerCase();
             //   final String jCompany = listTask.get(i).getJob_company().toLowerCase();
              //  final String jLoc = mDataList.get(i).getJob_location().toLowerCase();
                if (jTitle.contains(query) ) {
                    // make them also bold

                    filteredList.add(listTask.get(i));
                    highlightText(query,jTitle);
                  //  resultView.setVisibility(View.GONE);
                   // listTask.get(i).setText(sb);
                } else {
                //    resultView.setText("No results found for : " + query);
                  //  resultView.setVisibility(View.VISIBLE);

                }
            }
            final LinearLayoutManager  mLayoutManager = new LinearLayoutManager(getActivity());
            recyclerViewTask.setLayoutManager(mLayoutManager);
            taskAdapter = new TaskAdapter(getContext(),filteredList,this,this);
            recyclerViewTask.setAdapter(taskAdapter);
            taskAdapter.notifyDataSetChanged();

        }

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;
    }
jyo srijyo
  • 53
  • 6
  • https://stackoverflow.com/questions/59628149/how-can-we-have-searched-characters-colored-when-we-use-searchview-in-recyclervi – MMG May 11 '20 at 12:58
  • @MMG code in java – jyo srijyo May 11 '20 at 13:01
  • Java and Kotlin aren't so different, make an effort, you should manage it. All you really need to know is how to use `BackgroundColorSpan`, the rest is just finding the position of the query in the texts. – Nicolas May 11 '20 at 13:03
  • Does this answer your question? [How can we have searched characters colored when we use searchview in recyclerview?](https://stackoverflow.com/questions/59628149/how-can-we-have-searched-characters-colored-when-we-use-searchview-in-recyclervi) – MMG May 11 '20 at 13:05

0 Answers0