Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Spannable String into Html formatted String in Android?

Tags:

android

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

like image 919
Rahul Gupta Avatar asked Nov 05 '13 13:11

Rahul Gupta


2 Answers

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>
like image 74
Noob Avatar answered Nov 17 '22 04:11

Noob


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

String htmlString = Html.toHtml(spannedText);
like image 42
waqaslam Avatar answered Nov 17 '22 05:11

waqaslam