Search code examples
androidsearchfilterexpandablelistviewhighlighting

android - Highlight searched/filtered text in Expandable listview child items


For my college project I am using this sample-code example, project worked successfully and now I want to highlight searched text, but I don't kn how to implement it ,some of spanned-string example tried but does not support so any one please help me (as like as image)


Solution

  • to highlight searched term try below code:

            String m_filterVal = m_searchTerm;//Your search term suppose "p"
            String m_originalItemValue = ... //Your original whole string suppose "paint";
    
                int m_startPos = m_originalItemValue.toLowerCase(Locale.US).indexOf(m_filterVal.toLowerCase(Locale.US));
                int m_endPos = m_startPos + m_filterVal.length();
                if (m_startPos != -1) // This should always be true, just a sanity check
                {
                    Spannable m_spannable = new SpannableString(m_originalItemValue);
                    ColorStateList m_blueColor = new ColorStateList(new int[][] { new int[] {} }, new int[] { Color.BLUE });
                    TextAppearanceSpan m_highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, m_blueColor, null);
                    m_spannable.setSpan(m_highlightSpan, m_startPos, m_endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    m_tvCategoryItem.setText(m_spannable);//your textview name
                }
                else
                {
                    m_tvCategoryItem.setText(m_originalItemValue);
                }