7

I have a spannable string containing bold italics and underlined text ( it may contain more formatted text ) For Example : - abcdef dfdfdfdf dfgdfgfdgf dfgfdgdfgfdfgd

I want to convert this string into html formatted text such that, my final output contains tags such that the above string should become

abcdef dfdfdfdf dfgdfgfdgf dfgfdgdfgfdfgd

TextUtils.htmlEncode

is not working. Real World Scenario - You view a webpage and when you view its source it contains tags. Similar thing i want here

2 Answers 2

6

You need to use Html.toHtml() to convert your Spanned text into HTML string.

String htmlString = Html.toHtml(spannedText);
Sign up to request clarification or add additional context in comments.

3 Comments

If i use this, then if i have dfd in my spannable string, then the html is returned as <b>d</b><b>f</b><b>d</b>. I want that it should return <b>dfd</b>. Is there any way to achieve this ?
i think that's totally up to how the Html.toHtml method does the conversion. However what you may do is to use htmlString.replace("</b><b>", ""); to simplify the tags. Its a hack but will resolve this particular issue.
Ya did this only. Thanks anyway :)
6

this is how to convert string to html but consider not all html tags working on android .

Spanned htmlText = Html.fromHtml(text);
mytextView.setText(htmlText);

android supported only this html tags

<a href="...">
<b>
<big>
<blockquote>
<br>
<cite>
<dfn>
<div align="...">
<em>
<font size="..." color="..." face="...">
<h1>
<h2>
<h3>
<h4>
<h5>
<h6>
<i>
<img src="...">
<p>
<small>
<strike>
<strong>
<sub>
<sup>
<tt>
<u>

2 Comments

It seams that other tag work to such as &gt; and &lt; for > and <. And there is a bug as < is interpreted as a start tag and not as a lower than sign. see stackoverflow.com/questions/58718421/…
"Too many characters in character literal" error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.