HTML Code in TextView (in TableLayout)

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

HTML Code in TextView (in TableLayout)

Postby E-Nature » Thu Nov 26, 2009 5:01 pm

Hey all

is it possible to use HTML code in a TextView field?
I have a TableLayout view with some TextViews in it. the data I fetch sometimes has simple HTML tags in it. I would l like to display the HTML code properly. Can I do this in an easy way? or do I need to program a parser?

thanks for info! :)
dominik
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Top

Postby E-Nature » Wed Dec 16, 2009 11:26 am

[bump]
anyone?
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Postby SteffenL » Wed Dec 16, 2009 2:17 pm

Hello,

If I got you right, you can style text like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. TextView textView = (TextView)findViewById(R.id.textView);
  2.  
  3. CharSequence styledText = Html.fromHtml("<i>This</i> is some <b>styled</b> <s>text</s>");
  4.  
  5. textView.setText(styledText);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

If you need something more advanced, then how about using the WebView class?
Regards,
Steffen
SteffenL
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed Dec 16, 2009 9:52 am

Postby E-Nature » Wed Dec 16, 2009 2:37 pm

:D works!
thanks man!

the only thing which is not working is the <table> tag. I cannot seem to build a HTML table within a TextView. is that so? or is there a way around it?

http://developer.android.com/reference/android/text/Html.html :(
I just read that not all HTML tags are supported.
what's the best way to display a HTML table in a TextView?
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Postby SteffenL » Wed Dec 16, 2009 4:08 pm

I don't know but maybe you can pass a TagHandler when calling Html.fromHTML, and then handling the unknown "table" tag yourself somehow? Not sure how that would work.

Or maybe you have to manually create a TableLayout yourself, and in the end add TextViews into the cells. Then insert the HTML code for the cells into the TextViews.

Edit:
Or maybe you can use a GridView if your table is a grid. (example)
Regards,
Steffen
SteffenL
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed Dec 16, 2009 9:52 am

Postby revanth » Fri Jan 15, 2010 7:19 am

Hello E-Nature,

I also having the same problem.

I want to create a table in my textview..can u help me on this.

Thanks in advance
revandroid
revanth
Freshman
Freshman
 
Posts: 6
Joined: Wed Apr 29, 2009 3:51 pm
Location: Bangalore

Top

Postby E-Nature » Mon Jan 18, 2010 1:17 pm

I have found a solution for me. Although I think it's not the perfect solution - but hey, it works for me!

It only works for tables with 2 columns, though.

Here it comes:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class TextViewHTMLTest extends Activity {
  2.  
  3.  
  4.  
  5.         TextView tvTestAnzeige;
  6.  
  7.         TableLayout tlMain;
  8.  
  9.  
  10.  
  11.         /** Called when the activity is first created. */
  12.  
  13.         @Override
  14.  
  15.         public void onCreate(Bundle savedInstanceState) {
  16.  
  17.                 super.onCreate(savedInstanceState);
  18.  
  19.                 setContentView(R.layout.main);
  20.  
  21.  
  22.  
  23. String text = "Here comes the table with HTML formatting";
  24.  
  25.  
  26.  
  27.                 if (text.contains("</td>")) {
  28.  
  29.  
  30.  
  31.                         String[] textBeforeTable = text.split("<br><br>");
  32.  
  33.                         tvTestAnzeige = (TextView) findViewById(R.id.test);
  34.  
  35.                         tvTestAnzeige.setText(textBeforeTable[0]);
  36.  
  37.                         tvTestAnzeige.setPadding(0, 0, 0, 20);
  38.  
  39.  
  40.  
  41.                         tlMain = (TableLayout) findViewById(R.id.tlMain);
  42.  
  43.  
  44.  
  45.                         String[] trs = text.split("</tr><tr valign='top'>");
  46.  
  47.  
  48.  
  49.                         // Check, ob die Tabelle ein TH Element besitzt.
  50.  
  51.                         String firstTag = "th";
  52.  
  53.                         if (!trs[0].contains("</" + firstTag + ">")) {
  54.  
  55.                                 firstTag = "td";
  56.  
  57.                         }
  58.  
  59.  
  60.  
  61.                         String[] ths = trs[0].split("</" + firstTag + "><" + firstTag + ">");
  62.  
  63.                         String[] th1 = ths[0].split("<" + firstTag + ">");
  64.  
  65.                         String[] th2 = ths[1].split("</" + firstTag + ">");
  66.  
  67.  
  68.  
  69.                         String firstThColumn = "<b>" + th1[1].trim() + "</b>";
  70.  
  71.                         String secondThColumn = "<b>" + th2[0].trim() + "</b>";
  72.  
  73.  
  74.  
  75.                         /* Create a new row to be added. */
  76.  
  77.                         TableRow trTh = new TableRow(this);
  78.  
  79.                         trTh.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  80.  
  81.  
  82.  
  83.                         TextView tvFirstThCol = new TextView(this);
  84.  
  85.                         tvFirstThCol.setText(Html.fromHtml(firstThColumn));
  86.  
  87.                         tvFirstThCol.setPadding(1, 1, 10, 1);
  88.  
  89.                         trTh.addView(tvFirstThCol);
  90.  
  91.  
  92.  
  93.                         TextView tvSecondThCol = new TextView(this);
  94.  
  95.                         tvSecondThCol.setText(Html.fromHtml(secondThColumn));
  96.  
  97.                         tvSecondThCol.setPadding(10, 1, 1, 1);
  98.  
  99.                         trTh.addView(tvSecondThCol);
  100.  
  101.  
  102.  
  103.                         /* Add row to TableLayout. */
  104.  
  105.                         tlMain.addView(trTh, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  106.  
  107.  
  108.  
  109.                         for (int i = 1; i < trs.length; i++) {
  110.  
  111.  
  112.  
  113.                                 String[] tds = trs[i].split("</td><td>");
  114.  
  115.  
  116.  
  117.                                 String[] td1 = tds[0].split("<td>");
  118.  
  119.                                 String[] td2 = tds[1].split("</td>");
  120.  
  121.  
  122.  
  123.                                 String firstTdColumn = td1[1].trim();
  124.  
  125.                                 String secondTdColumn = td2[0].trim();
  126.  
  127.  
  128.  
  129.                                 /* Create a new row to be added. */
  130.  
  131.                                 TableRow trTd = new TableRow(this);
  132.  
  133.                                 trTd.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  134.  
  135.  
  136.  
  137.                                 TextView tvFirstTdCol = new TextView(this);
  138.  
  139.                                 tvFirstTdCol.setText(firstTdColumn);
  140.  
  141.                                 tvFirstTdCol.setPadding(1, 1, 10, 1);
  142.  
  143.                                 trTd.addView(tvFirstTdCol);
  144.  
  145.  
  146.  
  147.                                 TextView tvSecondTdCol = new TextView(this);
  148.  
  149.                                 tvSecondTdCol.setText(secondTdColumn);
  150.  
  151.                                 tvSecondTdCol.setPadding(10, 1, 1, 1);
  152.  
  153.                                 trTd.addView(tvSecondTdCol);
  154.  
  155.  
  156.  
  157.                                 /* Add row to TableLayout. */
  158.  
  159.                                 tlMain.addView(trTd, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  160.  
  161.  
  162.  
  163.                         }
  164.  
  165.  
  166.  
  167.                 } else {
  168.  
  169.                         tvTestAnzeige = (TextView) findViewById(R.id.test);
  170.  
  171.                         tvTestAnzeige.setText(text);
  172.  
  173.                 }
  174.  
  175.  
  176.  
  177.         }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


If anyone has a better solution I am more than happy to check it out 8)
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Alexa [Bot], Exabot [Bot] and 7 guests