Searching in Listview with searched textcolor highlighted in Listview Android

前端 未结 4 1457
终归单人心
终归单人心 2021-01-17 04:23

I Have a listview with arrayadapter .. i need to implement this in my music application ... help me out

4条回答
  •  独厮守ぢ
    2021-01-17 04:56

    Here is the Utility code that I use to highlight search key in search results,Hope this may help you to implement some part of your application

    public void setTextAndhighLightSearchKeytest(String searchKey,String searchResult, TextView textView) {
        if (!TextUtils.isEmpty(searchKey) && !TextUtils.isEmpty(searchResult)) {
            String searchResultLowerCase  = searchResult.toLowerCase(Locale.UK);
            String searchKeyLowerCase  = searchKey.toLowerCase(Locale.UK);
            int start = searchResultLowerCase.indexOf(searchKeyLowerCase);
            int end = start + searchKey.length();
    
            if (start > -1 && start < searchResult.length() && end < searchResult.length()) {
                SpannableStringBuilder builder = new SpannableStringBuilder(searchResult);
                builder.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                textView.setText(builder, BufferType.SPANNABLE);
            }
        }
    }
    

提交回复
热议问题