3

I've encountered a problem with formatting text in textview/editview. The problem is that if the text ends with a newline, the last line (empty) doesn't use any styles at all. Here's the code that is the reason: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/Layout.java#L1723 It doesn't matter unless the style is something like BulletSpan.

In my case it is exactly this problem - a bulletspan, an empty line and I want that empty span to be rendered. It seems that the only way to solve it is to add a zero-width space at the end of the EditView. (Android EditText: How to create an empty bullet paragraph by BulletSpan?) But then I'll have to deal with edits that could possible delete it.

So far the solution I found is to subclass SpannableStringBuilder and return it from my own Editable.Factory for my EditText. My SpannableStringBuilder will have to check if the last character is newline and add a zero-width space in this case. I will have to do that in both constructor and replace methods. The problem is - I have no idea what android code will actually do with my editable.

I have actually tried implementing something like that using some "actual" character and as result I had that character being duplicated, copied, pasted etc. I tried to manage the selection, not allowing SELECTION_START and SELECTION_END to ever be at the end of the text. Still the symbols were randomly duplicating when my edittext was getting or losing focus.

Is there any sane and reliable method of actually having the empty bullet displayed?

Possible other workarounds that I've tried:

  • Artificially drawing the bullet won't help - if the cursor is in the last position, it will be located at the left edge of the edittext, not at BulletSpan's left margin width.

  • Having a separate edittext for the last line only will be displaying stuff correctly. But I'll have a problem handling the backspace.

  • Subclassing EditText, not Editable - I can't handle selection going past the "trailing" character. Selection is applied directly to editable, not to EditText.

Community
  • 1
  • 1
aragaer
  • 17,238
  • 6
  • 47
  • 49

0 Answers0