Android Code Snippet Spannable Text to HTML

Recently I faced a problem involving spannable text where I needed to store an EditText content and later restore in a new View on screen.
It is a hard job as you can't store a formatted spannable text in a database or in a flat text. The solution is to convert the EditText content to html , store, and then convert again from html and show.
There are two Android java function those we can use to do this:

With a small implementation and the help of @Erel I developed a small java routine that could be useful to store and recover spannable texts, maintaining the types formatting. Attached also an apk with a sample code using the routine. To test:
- Open a formatted text in any app (browser, e-mail, etc)
- Copy the text
- Paste in the bottom edittext
- Click the button
- The formatted text will be copied to the edittext on the top

You can see that in the code there is a string variable called html... this could be stored in any string field using sqlite or even in a text file and recovered later... with the method fromHtml you can also convert a html text to spannable and use as a text in an edittext.
LIMITATIONS: not all html methods are implemented in Android... also, the alignment is lost when converting to html.

Sub Button1_Click
    Dim Jo As JavaObject

    Dim Cs As CSBuilder
    Dim Html As String

    Html = Jo.RunMethod("toHtml",Array(GetCharSequenceFromEditText(EditText2)))

    EditText1.Text = Jo.RunMethod("fromHtml",Array(Html))

End Sub

Sub GetCharSequenceFromEditText(et As EditText) As Object
    Dim jo As JavaObject = et
    Dim Test As Object
    Test = jo.RunMethod("getText", Null)
    Return jo.RunMethod("getText", Null)
End Sub

#If Java
import android.widget.TextView;
import android.text.Html;
import android.text.Spannable;
import android.text.Spanned;

    public static String toHtml(Spannable sourceText) {
        String genHtm = Html.toHtml(sourceText);
        return genHtm;
    public static Spanned fromHtml(String htmlString) {
         Spanned returnView = Html.fromHtml(htmlString);
        return returnView;
#End If


    9.4 KB · Views: 350