Innovator Innovator - 3 years ago 69
Java Question

How can I make bold and underline a string dynamically in Android code?

How can I make bold and italic the Branch Name:, Branch No:, Branch Phone No: and Branch Timings: in Java code? The database data should remain in simple format. These four are just headings of them. In this (current) case, it prints/displays the whole data in simple format and the user has a problem reading this data.

String branchInfoString = "" ;

// fetching all the database data in a list
List<BranchInfo> dbDataList = _dbHandler.getAllInfo() ;

// getting that data into a string that will be displayed in TextView
for(BranchInfo aBranch : dbDataList ){

branchInfoString = branchInfoString +

"Branch Name: " + aBranch.getBranchName() + "\n" +
"Branch No: " + aBranch.getBranchNo() + "\n" +
"Branch Phone No: " + aBranch.getBranchPhone() + "\n" +
"Branch Timings: " + aBranch.getBranchTiming() + "\n" +

"\n --------------------------------------- \n \n" ;

}

// showing all the branch data in a text_view
setContentView(R.layout.db_branch_list) ;
TextView dbDataView = (TextView)findViewById(R.id.db_branch_list) ;
dbDataView.setText(branchInfoString) ;

Answer Source

Yes, you can decorate the text in your TextView using HTML tags. That includes not just italics and bold, but also font color, and some other stuff. Something like this:

branchInfoHtmlString = branchInfoString + 

      "<b>Branch Name: " + aBranch.getBranchName() + "</b>\n" +
      "<i>Branch No: " + aBranch.getBranchNo() + "</i>\n" + 
      "<font >Branch Phone No: " + aBranch.getBranchPhone() + "</font>\n" + 
      "<font color="#FFDDDDDD">Branch Timings: " + aBranch.getBranchTiming() + "</font>\n" + 

      "\n --------------------------------------- \n \n" ;  

}

// showing all the branch data in a text_view
setContentView(R.layout.db_branch_list) ;
TextView dbDataView = (TextView)findViewById(R.id.db_branch_list) ;
dbDataView.setText(Html.fromHtml(branchInfoHtmlString)) ;

Not all HTML tags are supported, you can find the lists of tags here and here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download