How to Display HTML Text in Android TextView?

3.7k Views Asked by At

I Have an HTML Text.

Sales Resources

  • Drive to Collab Partner Presentation
  • Drive to Collab Program FAQs
  • Platform for Voice and Video Campaign

I would like to display this text in Android Text View. I have tried MyTagHandler to display, it is working fine while displaying, but the text formating is not good. If the bulleted text has 2 lines then the second line is coming from first, but i need space upto bullet. how can i handle this? Problem Image

2

There are 2 best solutions below

0
On

i guess like this...

TextView foo = (TextView)findViewById(R.id.foo);
foo.setText(Html.fromHtml("your html text"));
0
On

You can do that in 2 ways.

  1. First you create a HTML Script for it.

Once you are done with it, then set that to textview as shown below. (preferable for smaller text)

myTextView.setText(Html.fromHtml("Html Script"));
  1. Create a html file and put it in res/raw.

Then use the follwoing code.

myTextView.setText(Html.fromHtml(getHtmlText())); 

Now define getHtmlText() method as below:

private String getHtmlText(){
 InputStream inputStream = getResources().openRawResource(R.raw.my_html_file);
 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 int i;
 try {
 i = inputStream.read();
  while (i != -1)
  {
   byteArrayOutputStream.write(i);
   i = inputStream.read();
  }
  inputStream.close();
  } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
  return byteArrayOutputStream.toString();
}