khalid-hussain / HisnulMuslim

This repository is about an Android app which would include all the dua from Hisnul Muslim.
53 stars 27 forks source link

Add bold effect to user filter text in Dua Group list #11

Open khalid-hussain opened 9 years ago

khalid-hussain commented 9 years ago

Case in point:

khalid-hussain commented 9 years ago

The following StackOveflow question seems to give a hint but I don't see how I can fit it into the current code: Highlight searched text in ListView items

mdali92 commented 9 years ago

1) Create global variable :

   private CharSequence search_txt = "";

2) Edit performFiltering method by adding the following right at the beginning :

  search_txt = constraint;

3) Finally, edit getView method by adding the following lines :

        holder.tvDuaName.setText(p.getTitle());
        //highlight(search_txt.toString(),holder.tvDuaName.getText().toString());
        String filter = search_txt.toString();
        String itemValue = holder.tvDuaName.getText().toString();

        int startPos = itemValue.toLowerCase(Locale.US).indexOf(filter.toLowerCase(Locale.US));
        int endPos = startPos + filter.length();

        if (startPos != -1) // This should always be true, just a sanity check
        {
            Spannable spannable = new SpannableString(itemValue);
            ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] {        Color.BLUE });
            TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);

            spannable.setSpan(highlightSpan, startPos, endPos,    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.tvDuaName.setText(spannable);
        }
        else
            holder.tvDuaName.setText(itemValue);

P.S : There is one correction. In the if else statement right after the line :

            ViewHolder holder;

After you have completed the "if" part, you forgot to add the "else" and have begun without it. Add the "else" otherwise the logic is wrong.

khalid-hussain commented 9 years ago

JazakAllahu khayran.

UPDATE Issue has been re-opened. Future iterations should work for recyclerView.