Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) ;
like image 938
Innovator Avatar asked Jan 30 '13 05:01

Innovator


2 Answers

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.

like image 56
Anton Cherkashyn Avatar answered Sep 24 '22 01:09

Anton Cherkashyn


Use HTML content to format your text for example:

branchInfoString = branchInfoString + 

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

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

dbDataView.setText(Html.fromHtml(branchInfoString ));
like image 29
TheWhiteRabbit Avatar answered Sep 24 '22 01:09

TheWhiteRabbit