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

How can I make bold and italic name Branch: , Branch number: , Branch number: and Filtering time: in Java code? Database data should remain in a simple format. These four are just the headings of them. In this (current) case, it prints / displays all the data in a 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) ;
+5
source share
5 answers

Yes, you can decorate text in a TextView with HTML tags. This includes not only italics and bold, but also font color and some other things. Something like that:

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)) ;

HTML- , .

+9

HTML- , :

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 ));
+4

You can format the string and set it in text mode

String some_string = Html.fromHtml(YourHTMLFromattedString)
dbDataView.setText(some_string);
0
source

use Html.fromHtml (String source) and tag <b></b>and <i></i>to display some text in bold and italics as

for(BranchInfo aBranch : dbDataList ){

branchInfoString = branchInfoString + 

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

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

}

//your code ...

dbDataView.setText(Html.fromHtml(branchInfoString)) ;
0
source

You will need to create a tableView with several rows to be added at runtime. The first row will contain the column headings.

Ref.

<TableLayout android:id="@+id/class">
    <TableRow android:id="@+id/headers">
        <TextView android:id="@+id/branch_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:typeface="serif">
        <TextView android:id="@+id/branch_no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:typeface="serif">
        <TextView android:id="@+id/branch_phone_no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:typeface="serif">
        <TextView android:id="@+id/branch_timings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:typeface="serif">
    </TableRow>
</TableLayout>

and to add data to this table, in the class you would add something like this:

TableLayout layout =     (TableLayout)LayoutInflater.from(canvas.getContext()).inflate(R.layout.class, null);

TableRow headerRow = (TableRow) layout.findViewById(R.id.header);
TextView branchName = (TextView) layout.findViewById(R.id.branch_name);
TextView branchNo = (TextView) layout.findViewById(R.id.branch_no);
TextView branchPhoneNo = (TextView) layout.findViewById(R.id.branch_phone_no);
TextView branchTimings = (TextView) layout.findViewById(R.id.branch_timings);

branchName.setText("Branch Name");
branchName.setTypeFace(null, Typeface.BOLD);

branchNo.setText("Branch No");
branchNo.setTypeFace(null, Typeface.BOLD);

branchPhoneNo.setText("Branch Phone No");
branchPhoneNo.setTypeFace(null, Typeface.BOLD);

branchTimings.setText("Branch Timimgs");
branchTimings.setTypeFace(null, Typeface.BOLD);


for (int i=0; i<noOfRows; i++) {
    TableRow dataset = new TableRow(context);
    TextView branchName = new TextView(context);
    TextView branchNo = new TextView(context);
    TextView branchPhoneNo = new TextView(context);
    TextView branchTimings = new TextView(context);

    branchName.setText("SampleBranchName" + i);
    branchNo.setText("SampleNumber" + i);
    branchPhoneNo.setText("SamplePhoneNumber" + i);
    branchTimings.setText("SampleTimings" + i);

    dataset.addView(branchName);
    dataset.addView(branchNo);
    dataset.addView(branchPhoneNo);
    dataset.addView(branchTimings);

    layout.addView(dataset);
}
0
source

All Articles