0

HI everyone, I have a bunch of text in an Edit text that I have set up to be styled(strike through only for the moment) using the setSpan method in Android. This seems to work fine.

The trouble I am having is that all the styling seem to get cancelled once I close that activity. That is when I load up the activity again , it just has plain text and none of the styling I had applied using the setSpan().

Note: All of my text get stored in a Database.

I have attached all the code for the styling, let me know if you need to see any more bits of code.

     private void doStrick() {

    int selectionStart = mBodyText.getSelectionStart();
    styleStart = selectionStart;
    int selectionEnd = mBodyText.getSelectionEnd();
    // check for boo-boo's 
    if (selectionStart > selectionEnd){
        int temp = selectionEnd;
        selectionEnd = selectionStart;
        selectionStart = temp;
    }
    Spannable str = mBodyText.getText();
    str.setSpan(new StrikethroughSpan(),selectionStart, selectionEnd, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    }

Is that some bit of code I need to be adding to save the styling?

EDIT based on answer:

    Calendar cal=Calendar.getInstance();
    String date_time=String.format("%1$te %1$tB %1$tY,%1$tI:%1$tM:%1$tS %1$Tp",cal);
    float Textsize = mBodyText.getTextSize();
    String title = mTitleText.getText().toString();
    String body = Html.toHtml(mBodyText.getText());

    if (mRowId == null) {
        long id = mDbHelper.createNote(title, body, date_time, Textsize);

        if (id > 0) {
            mRowId = id;
        }
    } else {
        mDbHelper.updateNote(mRowId, title, body, date_time, Textsize);
        Log.d("MYTAG", "updateing note");
        updateWidget();

And where I populate the fields:

   Cursor note = mDbHelper.fetchNote(mRowId);
        startManagingCursor(note);
        mTitleText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
        mBodyText.setText(Html.fromHtml(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY))));
        mBodyText = (EditText) findViewById(R.id.body);
        float size =                 note.getFloat(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TEXT_SIZE));
        mBodyText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
DrkStr
  • 1,752
  • 5
  • 38
  • 90

1 Answers1

2

Is that some bit of code I need to be adding to save the styling?

Yes.

Presumably, right now, you are just saving thisIsYourEditText.getText().toString() to your database, then using thisIsYourEditText.setText(stringThatYouLoadBackOutOfYourDatabase) to populate the EditText.

Instead, you need to use Html.toHtml(thisIsYourEditText.getText()) to try to convert your styled text into HTML, and then use thisIsYourEditText.setText(Html.fromHtml(stringThatYouLoadBackOutOfYourDatabase)) to convert that HTML back into styled text.

Note that toHtml() and fromHtml() do not handle all possible CharacterStyles, nor are they guaranteed to do all of the styling correctly round-trip (i.e., the string generated by toHtml() may not match the string you started with before the fromHtml() call).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @DrkStr: No -- if you were storing the `EditText` contents as a string, HTML is just another string. – CommonsWare Jul 13 '13 at 17:34
  • Hey, I have tried the fromHTM and toHTML and it did not make a difference.Code for the modified bits in the edit. – DrkStr Jul 13 '13 at 17:45
  • @DrkStr: Did you look at the results from `toHtml()` to see if the HTML contained markup that lined up with your spans? Also, the only span that I see from your original question is `StrikethroughSpan`, and that is one that `toHtml()` supports, but not `fromHtml()`, at least by my read of the source code to the `Html` class: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/text/Html.java – CommonsWare Jul 13 '13 at 17:53
  • just had a read through the link ... my other span was bullets and that is not supported either, so bummer for me :( . Anyz , thank for helping out man. – DrkStr Jul 13 '13 at 18:04
  • I have another bit of code I am having trouble with , wondering if you could have a look at that and help me out there as well. Cheers.http://stackoverflow.com/questions/17622938/launching-an-activity-from-a-widget-with-a-configure-activity – DrkStr Jul 13 '13 at 18:06