0

I'm trying to bold the specific text in listview that contains ":". I want to bold the word underlined in the image below that contains ":" but I'm having trouble getting all the message. Please see my current code below. I appreciate for your any response.

enter image description here

ArrayList<String> list_items = new ArrayList<String>();
public  void listview_refresh(){
    arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list_items);

    ContentResolver cResolver = getContentResolver();
    Cursor smsInboxCursor = cResolver.query(Uri.parse("content://sms/inbox"),null,null,null,"date desc");
    int indexBody = smsInboxCursor.getColumnIndex("body");
    if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
    do{
        str = smsInboxCursor.getString(indexBody) ;
        arrayAdapter.add(str);

        String[] separated = str.split(":");
        String separate = separated[1];

        arrayAdapter.replaceAll(separate, "<b>" + separate + "</b>"); //errorline

    }while (smsInboxCursor.moveToNext());
}
McLarvel
  • 31
  • 6
  • check if you have an answer here. – KalanaChinthaka Jul 07 '20 at 05:56
  • https://stackoverflow.com/a/30546555/13685592 – KalanaChinthaka Jul 07 '20 at 05:56
  • I already check this but where did the myTextview came from? custom listview? myTextView.setText(Html.fromHtml(sentence)); @KalanaChinthaka – McLarvel Jul 07 '20 at 06:01
  • @DagzRV you are setting the text somewhere using this adapter where is the code ? on that textView you should call yourTextView.setText(Html.formatHtml(yourStringfromAdapterHere)); – AgentP Jul 07 '20 at 06:06
  • https://i.stack.imgur.com/sgiok.jpg Thanks for your response this textview is connected to my lisview but my textview.xml is on the separate xml and also did not use textview to bold the text I use listview @KalanaChinthaka – McLarvel Jul 07 '20 at 06:22

1 Answers1

0

Add string as Html within the adapter list. I have modified your code and added Html.fromHtml(..) to make text to use HTML code within.

ArrayList<String> list_items = new ArrayList<String>();
public  void listview_refresh(){
arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list_items);

ContentResolver cResolver = getContentResolver();
Cursor smsInboxCursor = cResolver.query(Uri.parse("content://sms/inbox"),null,null,null,"date desc");
int indexBody = smsInboxCursor.getColumnIndex("body");
if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
do{
    str = smsInboxCursor.getString(indexBody) ;
    
    String[] separated = str.split(":");
    String separate = separated[1];

    str = str.replaceAll(separate, "<b>" + separate + "</b>"); //errorline
    arrayAdapter.add(Html.fromHtml(str));
}while (smsInboxCursor.moveToNext());
}
Ashish Karn
  • 1,127
  • 1
  • 9
  • 20