Search code examples
javahtmlandroid-studiostringbuilder

How to Make my String Builder labels Id, Name,Address Bold


I am displaying a list from database i want the labels to be bold.

   Cursor cursor = GetAllData();
           StringBuilder stringBuilder =new StringBuilder();
           while(cursor.moveToNext()){
               stringBuilder.append("Id :" + cursor.getString(0)+ " | " +"Name :"+ 
     cursor.getString(1)+" | "+"Address :" + cursor.getString(2)+" | "+"Phone :" + 
      cursor.getString(3)+"\n"+"--------------------------------------------------"+"\n");
           }
           textView.setText(stringBuilder);

That is Id,Name,Address must be bolded what to add to make it bolder.


Solution

  • You are setting a textView. Add HTML bold tags. Also, that is terrible code. You are nesting StringBuilder (because String + String uses another StringBuilder). Also, it's a bad idea to assume the line separator is \n. Fixing it should be simple enough. Something like,

    Cursor cursor = GetAllData();
    StringBuilder sb = new StringBuilder();
    while (cursor.moveToNext()) {
        sb.append("<b>Id</b> :").append(cursor.getString(0));
        sb.append(" | <b>Name</b> :").append(cursor.getString(1));
        sb.append(" | <b>Address</b> :").append(cursor.getString(2));
        sb.append(" | Phone :").append(cursor.getString(3));
        sb.append(System.lineSeparator());
        sb.append("--------------------------------------------------");
        sb.append(System.lineSeparator());
    }
    textView.setText(Html.fromHtml(sb));