0

I am trying to put color and bold up on a word that matches the word from a sentence in ArrayList items/value. what I have done so far is as below... so I have a String variable called filter "Hello" now I want to iterate through ArrayList itemList and find "Hello" from ArrayList items and make it bold and blue.

String filter = "Hello";
    String itemValue = "I just want to say Hello world! Hello";

    itemList = new ArrayList<String>();
    for(int i = 0; i<10; i++)
    {
        itemList.add("I just want to say Hello world! Hello");
    }
    sentence = new ArrayList<String>();
    for(int j=0; j<itemList.size(); j++)
    {
        int startPos = itemList.get(j).toString().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);
            tv.setText(spannable);

            sentence.add(itemValue);

            ListView lv = (ListView)findViewById(R.id.listView1);
            //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
              //      android.R.layout.simple_list_item_1, android.R.id.text1, sentence);
            MyPerformanceArrayAdapter adapter = new MyPerformanceArrayAdapter(this, spannable, counterArrayList);
            lv.setAdapter(adapter);
        }
        else
            tv.setText(itemValue);                 
    }             

its not working can you please help me

Jack
  • 151
  • 1
  • 4
  • 21

1 Answers1

3

You should make another constructor passing it another argument String filter which will contain your string to be bold.

Assign it to another data member in the arrayAdapter

Now while displaying the view, You should replace the filter in every sentence. i.e. sentence.replace(filter, "<b>" + filter + "</b>")

And show it into the textView like this myTextView.setText(Html.fromHtml(sentence));

After that all you have to do is Reset Adapter

Salmaan
  • 3,543
  • 8
  • 33
  • 59