Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Html text to plain text in android?

I required convert HTML text to Plain text in String form.

String mHtmlString = "<p class="MsoNormal" style="margin-bottom:10.5pt;text-align:justify;line-height: 10.5pt"><b><span style="font-size: 8.5pt; font-family: Arial, sans-serif;">Lorem Ipsum</span></b><span style="font-size: 8.5pt; font-family: Arial, sans-serif;"> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<o:p></o:p></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-size: 8.5pt; font-family: Arial, sans-serif;"> </span><span style="font-family: Arial, sans-serif; font-size: 8.5pt; line-height: 10.5pt; text-align: justify;">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</span></p>"

What I did so far:

TextView textView = (TextView) findViewById(R.id.textView);
String plainText = Html.fromHtml(mHtmlString).toString()
textView.setText(plainText);

Bad luck, not working for nested HTML.

Any help would appreciate.

like image 635
Hiren Patel Avatar asked Mar 22 '14 04:03

Hiren Patel


People also ask

Can we use HTML in Android Studio?

Step 1: To add a local HTML file into your Android project there must be an asset folder in it. To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder.

What is plain text converter?

A plain text file uses a very basic character set and contains no formatting - resulting in clean HTML code for use in web sites and/or ebooks. This free instant text converter tool converts text from MS Word documents produced by Microsoft Word and similar MS Office software, such as. doc or.


1 Answers

I am giving my answer.

String mHtmlString = "<p class="MsoNormal" style="margin-bottom:10.5pt;text-align:justify;line-height: 10.5pt"><b><span style="font-size: 8.5pt; font-family: Arial, sans-serif;">Lorem Ipsum</span></b><span style="font-size: 8.5pt; font-family: Arial, sans-serif;"> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<o:p></o:p></span></p> <p class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-size: 8.5pt; font-family: Arial, sans-serif;"> </span><span style="font-family: Arial, sans-serif; font-size: 8.5pt; line-height: 10.5pt; text-align: justify;">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</span></p>";

Set Html text string on TextView:

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(Html.fromHtml(Html.fromHtml(mHtmlString).toString()));

Hope this will help you.

like image 135
Hiren Patel Avatar answered Sep 19 '22 14:09

Hiren Patel