How to convert Html text to plain text in android?
Asked Answered
C

4

17

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.

Customer answered 22/3, 2014 at 4:6 Comment(4)
Desc.setText(desc);... Uh don't think you mean that.Bankbook
what's problem u face ?Agio
@Agio i can not convert plain text from html text. but i got solution, pls see my answer.Customer
I have an html String that comes from API. I want to convert that into a simple String. Html.fromHtml().toString is deprecated and also doesn't show the tags anymore.Farica
C
51

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.

Customer answered 22/3, 2014 at 9:42 Comment(3)
Why did you nest Html.fromHtml()?Gaiety
@TaylorKline, Actually having nested HTML tags.Customer
With single I got : "&lt;div&gt;<div><div><div><div><div>&lt;/div&gt;</div></div></div></div></div>" and with nested I got: ""............. therefore we ned to nest itSic
T
8

If you're looking for removing the html tags from html then use Jsoup( http://jsoup.org)

 String textFromHtml = Jsoup.parse(MY_HTML_STRING_HERE).text();
 TextView desc = (TextView) dialog.findViewById(R.id.description);
 desc.setText(textFromHtml);
Tuttifrutti answered 22/3, 2014 at 4:44 Comment(2)
...if you don't mind your app expanding by half a megTiernan
This is much better than Html.fromHtml for more complex Html.Jollenta
C
6

This works for me:

    Spanned spanned = Html.fromHtml(textWithMarkup);
    char[] chars = new char[spanned.length()];
    TextUtils.getChars(spanned, 0, spanned.length(), chars, 0);
    String plainText = new String(chars);

I use it with simple tags like <b> and <i>. Did not test with more complex HTML.

Cr answered 6/11, 2016 at 9:52 Comment(2)
Very nice and efficient solution, no library needed.Intwine
Superb solution......Thanks a lotAime
F
3

No one mentioned the superb Kotlin extension function to do this:

just use it like:

"yourHtmlString".parseAsHtml()

for more info:

parseAsHtml()

Fiscus answered 19/6, 2022 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.