Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting text to HTML doesn't work for bold and italic #1

Closed
jakubkrolewski opened this issue Jun 10, 2015 · 6 comments
Closed

Converting text to HTML doesn't work for bold and italic #1

jakubkrolewski opened this issue Jun 10, 2015 · 6 comments
Assignees

Comments

@jakubkrolewski
Copy link

  1. I press "bold" in the demo activity
  2. I type "Hello"
  3. I see bold text "Hello"
  4. mRTMessageField.getText(RTFormat.HTML) returns just plain text without any formatting (so Hello instead of <b>Hello</b>
@jakubkrolewski jakubkrolewski changed the title Converting text to HTML doesn't work at all Converting text to HTML doesn't work for bold and italic Jun 10, 2015
@1gravity
Copy link
Owner

I can't reproduce the issue, it works perfectly for me, mRTMessageField.getText(RTFormat.HTML) returns html for Italic and Bold. The rich text editor is used in thousands of installations, if there was indeed that issue, my users would have complained already.
You can save the text in the demo app and reload it which will show you that the formatting is indeed saved.

@jakubkrolewski
Copy link
Author

The saved HTML file is completely empty, but that's another problem.
I tested on Nexus 5, Android 4.4.4.
I debugged the problem a little bit. In ConverterSpannedToHtml class, the line 251:
List<CharacterStyle> spanList = Arrays.asList(text.getSpans(start, end, CharacterStyle.class));
returns a single element list with
0 = {StyleSpan@830046449792} mStyle = 1.
I don't know why it's not the BoldSpan as it was initially inserted, but that's what's there.
I propose to fix the problem by generating a html tag both from the BoldSpan and StyleSpan. It not only fixes the problem I describe, but also make the ConverterSpannedToHtml a better general purpose tool.

@1gravity
Copy link
Owner

My Nexus 5 (although running Android M) has no such issue. Also tested it on an S3 running KitKat.

I don't want a workaround for an issue if the root cause isn't known and might lead to other (yet unknown) issues. As I said thousands of users use this component and no one has ever reported anything like this so let's find out what's special about your device.

Anything out of the ordinary on your device? rooted? custom 4.4.4? any suspicious apps installed? do you use a custom font?
What happens if you rotate the screen to landscape, is the bold text still there?

BTW I suggest you contact me by email to solve this: 1gravityllc@gmail.com

@jakubkrolewski
Copy link
Author

After some more tests I've found the proper scenario to reproduce the problem:

  1. Enable "don't keep activities" in the developer options
  2. Start demo activity
  3. Press "bold"
  4. Type "Hello"
  5. Press HOME
  6. Go back to the demo application

That should be it.

The problem is that the onSaveInstanceState method in TextView uses the TextUtils#writeToParcel method for saving text and this method can handle only built-in spans. I don't think that it's possible to extend this mechanism for custom spans.
I see two possible solutions for the problem of saving and restoring state:

  1. Copy the whole spans parcelling mechanism and modify the copied code to support custom spans
  2. Use the built-in mechanism, but after restoring text convert all the spans to custom versions (I know that we can easily convert a StyleSpan to BoldSpan, but I don't know if that's so easy for other spans in the library).

@1gravity 1gravity reopened this Jun 15, 2015
@1gravity 1gravity self-assigned this Jun 15, 2015
@1gravity
Copy link
Owner

I went for option 3 ;-).

The proper way to handle this is to save the view state in onSaveInstanceState and restore it in onRestoreInstanceState. By saving the text as html and restoring it from that html we can retain all custom span objects.

Fixed with 9ac8f6a

@jakubkrolewski
Copy link
Author

Indeed, the third solution sounds better:)
It works great now. Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants