How to extract HTML style text from EditText in Android?

I am using HTML.fromHTML(...) to style my EditText text in Android. I need to pass the stylized text back as a result to another action. However, when I use the intention to pass the contents of the EditText, I cannot figure out how to maintain the HTML style of the source text.

As an example, suppose the source text in EditText:

Today 21 st

When I extract the text using edittext.getText() and send it back, the result is the following text:

Today is the 21st

Is there a way to extract an HTML style string from an EditText?

+7
source share
3 answers

You can send the HTML text itself, and then call Html.fromHTML in the action to which you pass the text. fromHTML is for use with text to be displayed on the screen

+3
source

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

 String htmlString=Html.toHtml(edittext.getText()); 
+14
source

This will not work if your text is not stretched:

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

You need to drop it by creating the instance first:

 String htmlString = Html.toHtml(new SpannableString(edittext.getText())); 
+3
source

All Articles