Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract HTML styled text from an EditText in Android?

I am using HTML.fromHTML(...) to style the text of an EditText in Android. I need to pass the styled text back as a result to another activity. However, when I use an intent to pass the contents of the EditText I am unable to figure out how to retain the HTML style of the original text.

As an example, suppose that the original text in the EditText is:

Today is the 21st

When I extract the text of using edittext.getText() and send it back as a result the resulting text is:

Today is the 21st

Is there a way to extract the HTML styled string from the EditText?

like image 645
Mandel Avatar asked Jul 01 '11 05:07

Mandel


3 Answers

Use this to get the HTML of the styled text. You can use the HTML in EditText, TextView or WebView

String htmlString=Html.toHtml(edittext.getText());
like image 102
Walid Hossain Avatar answered Nov 13 '22 12:11

Walid Hossain


This won't work if your text is not spanned:

edittext.setText("");
//error here:
String htmlString = Html.toHtml((Spanned) edittext.getText()); 

You need to cast it by creating an instance first:

String htmlString = Html.toHtml(new SpannableString(edittext.getText()));
like image 29
live-love Avatar answered Nov 13 '22 14:11

live-love


You can send the HTML text itself and then call Html.fromHTML in the activity to which you are passing this text. fromHTML is meant to be used for text which has to be displayed on the screen

like image 4
Abhinav Avatar answered Nov 13 '22 14:11

Abhinav