5

How to display HTML table with rows and columns in WebView in Android.

Give me some example.

laalto
  • 150,114
  • 66
  • 286
  • 303
Android_prog
  • 133
  • 2
  • 4
  • 11

3 Answers3

7

Create an HTML template

    String myTable = "<table border=1>" +
        "<tr>" +
        "<td>row 1, cell 1</td>" +
        "<td>row 1, cell 2</td>" +
        "</tr>" +
        "<tr>" +
        "<td>row 2, cell 1</td>" +
        "<td>row 2, cell 2</td>" +
        "</tr>" +
        "</table>";

and load into your WebView

myWebView.loadDataWithBaseURL(null, myTable, "text/html", "utf-8", null);
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 1
    It works fine, but the problem is I am unable to use other features of HTML table like I want to reduce the cellspacing between rows, or I want to add some color to cell spacing called Border Color, which I am not able to do with Android. Can u give me sample code for table which appears exactly like table with lines in middle of columns and rows. Thanks in Advance, – Android_prog Aug 21 '10 at 18:40
  • 1
    In landscape mode the text disappears. if size is bigger. Not able to put scroll within webview. Any idea how to implement scroll in webview – mainu Aug 01 '13 at 05:17
  • I wrote: myWebView.loadData(myTable, "text/html", "utf-8"); and the page wasn't loaded. I replaced it to myWebView.loadDataWithBaseURL(null, myTable, "text/html", "utf-8", null); as mentioned, and the page was loaded! – M. Marmor Dec 24 '20 at 14:13
2

Its also working for me:

String tag = "<table border=1>" +
                "<tr>" +
                   "<td>row 1, cell 1</td>" +
                   "<td>row 1, cell 2</td>" +
                "</tr>" +
                "<tr>" +
                   "<td>row 2, cell 1</td>" +
                   "<td>row 2, cell 2</td>" +
                "</tr>" +
             "</table>";

((WebView) findViewById(R.id.web)).loadData(tag, "text/html", "utf-8");
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
0

I think that this is an issue related to Android web view's in 2.2 and 2.3. The table tags like width, cell-spacing, and cell-padding are not supported in some web views and thus should not be used. If you want to style a table with webView.loadData(), use inline stlying with the style tag only.

Table border should also be removed as well.

Droid Chris
  • 3,455
  • 28
  • 31