Search code examples
androidhtmlsqlitesavestrikethrough

Strike through text is not saved to SQLite database


I encountered the problem that when using StrikethroughSpan, the strike through text is not saved to the SQLite database of my app. It works fine with other spans though, particularly with StyleSpan and UnderlineSpan - the bold/italic/underlined text is correctly saved to the DB and displayed by CursorAdapter. I'm using HtmlCompat for this purpose. But the strike through text is shown without formatting, as plain text.

My web research on this matter didn't give any results. What is the reason for such behavior and is there a way to solve the problem?

My code to save the spanned text to DB:

ContentValues values = new ContentValues();
        NoteCursorAdapter cursorAdapter = new NoteCursorAdapter(this, null);

        String newHtmlString = HtmlCompat.toHtml(noteText, TO_HTML_PARAGRAPH_LINES_CONSECUTIVE);

        values.put(NoteEntry.COLUMN_NOTE_TEXT, newHtmlString);

in activity:

@Override
public void bindView(View view, Context context, Cursor cursor) {
            ...
        String htmlFormString = cursor.getString(noteBitmapColumnIndex);
        Spanned spannedText = HtmlCompat.fromHtml(htmlFormString, FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE);
        noteBitmap.setText(spannedText);
}

in CursorAdapter:

@Override
public void onLoadFinished(...) {
    ...

    htmlString = cursor.getString(textColumnIndex);
    realText = HtmlCompat.fromHtml(htmlFormString, FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE);    
    mNoteText.setText(realText);

The html string which is put into ContentValues (newHtmlString) shows the corresponding HTML tags:

I/System.out: <p dir="ltr"><b><b><strike>Word</strike></b></b></p>

but the formatting is not displayed.


Solution

  • Html class is not too well documented in terms of tags it supports. Checked the source code of it from Android 5 and Android 9. Android 9 version supports strike-through both ways (ref) while Android 5 only outputs it but does not read it in fromHtml() (ref). Did not dig through when exactly it was changed.

    You can probably work around this by supplying a custom tag handler to fromHtml. Here's an example: Android: How to use the Html.TagHandler?