3

I want to display text inside textview using this code:

Html.fromHtml("<html><body><table style=width:100%><tr><td><B>No</td><td><B>Product Name</td><td><B>Qty</td><td><B>Amount</td></tr></body></html>");

But result is not in correct format result look like this:

NoPRoductNameQtyAmount

please suggest what i am doing wrong in this code.

Abhishek Kotiyal
  • 283
  • 1
  • 5
  • 17
  • First: html tags are always lower cases only. So B must become b. Also you should close all the tags so for example Amount . Fix these and then see if it works – Lelio Faieta May 19 '15 at 12:14
  • Thanks @eduyayo.... i am doing same as above post suggest but output is not correct. – Abhishek Kotiyal May 19 '15 at 12:15
  • @LelioFaieta: I don't know what `fromHtml()` does, but _HTML itself_ allows both upper and lower case in tag names. You also need to provide close tags for the _``_ and _``_ elements. The close tags for the other elements are optional. Again, the `fromHtml()` method may impose further restrictions that I am unaware of.
    – Nisse Engström Sep 08 '15 at 19:20

4 Answers4

11

fromHtml() does not support <table> and related tags. Your choices are:

  1. Reformat your text to avoid tables

  2. Use WebView to render your HTML table

  3. Use native widgets and containers (e.g., TableLayout) for your table

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Instead of using html table inside a TextView I've solved formatting normal text into a table like text, adding white spaces into the text to have a tidy structure.

This is the code:

String lines[] = getItem(position).toString().split("\n");
String print = "";
String parts[] = null;
String tmp = "";
int weight = 0;
for (int i=0; i < lines.length; i++) {
   if (!lines[i].equalsIgnoreCase("")) {
       parts = lines[i].split(":", 2);
       tmp = "";
       Paint textPaint = text.getPaint();
       float width = textPaint.measureText(parts[0]);
       float wslength = textPaint.measureText(" ");
       for (int j = 0; j < (220 - Math.round(width))/Math.round(wslength); j++) {
           tmp = tmp + " ";
       }
       if (print.equalsIgnoreCase("")) {
           print = print + parts[0] + tmp + Html.fromHtml(""+parts[1]+"");
       } else {
           print = print + "\n" + parts[0] + tmp + parts[1];
       }
   }
}

You can find more explanations here: http://blog.blupixelit.eu/convert-text-to-table-in-android-sdk/

Hope this helps.

Carmelo
  • 1
  • 1
0

How about change the way to just render table by webview, the other tags render by textview ? Recently i finish a demo to overcome this. So we first need separate<table> with others supported tags, which will like change "<p>**<p> blabala <table>balabla </table> blabla <p>**<p>" to three separated strings

  • <p>**<p> blabala
  • <table>balabla </table>
  • blabla <p>**<p>

Then only <table> included tags render by webview, the others by textview And the result in android will be like:

 <ScrollView> 
   <LinearLayout>
    <TextView> 
    <WebView>  --- let the webview ATMOST measured
    <TextView> 

So every thing goes fine as i think. check this commit for detail

Bruce too
  • 21
  • 7
-2

HtmlTextView recently added basic support for HTML tables. It's limited, but will do the trick if all you have to worry about is <table>, <td>, <tr>, and <th>.

seato
  • 2,061
  • 2
  • 15
  • 18