Android EditText: How to create an empty BulletSpan bullet paragraph

I use the same name with this question because, in my opinion, my question is very similar to this question, I read and tested the accepted answer very carefully, however the accepted answer does not work for me. Let me describe my question:

My code looks like this:

EditText myEdit = (EditText) this.findViewById(R.id.myedit); myEdit.setText("a\nb\n"); Spannable s = myEdit.getText(); s.setSpan(new BulletSpan(30), 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); s.setSpan(new BulletSpan(30), 2, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); s.setSpan(new BulletSpan(30), 4, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); myEdit.setText(s); 

What I want to see:

  • but
  • b
  • [I want to see the 3rd bullet here, but it does not appear]

I tried Spannable.SPAN_INCLUSIVE_INCLUSIVE , Spannable.SPAN_INCLUSIVE_EXCLUSIVE , Spannable.SPAN_EXCLUSIVE_INCLUSIVE , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE , but none of these flags work for me.

And if I use these codes:

 EditText myEdit = (EditText) this.findViewById(R.id.myedit); myEdit.setText("a\nb\nc"); Spannable s = myEdit.getText(); s.setSpan(new BulletSpan(30), 0, 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); s.setSpan(new BulletSpan(30), 2, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); s.setSpan(new BulletSpan(30), 4, 5, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); myEdit.setText(s); 

Then I get the expected result:

  • but
  • b
  • from

I am working on a text editor, when the user clicks the bullet icon, I need to show a blank mark, but now I'm not sure what the problem is, because I want to create a new blank BulletSpan (only with a dot, but there are no characters after it), but if in the beginning and end of the range there are no characters, the point is not displayed.

+7
android android-edittext spannablestring spanned bulletedlist
source share
3 answers

This is an ugly solution, but I did not find a better one - try adding an empty character at the end (something like zero-width space). This gives the result you need (at least visually):

 public void setBulletText(EditText myEdit, String text) { String[] lines = TextUtils.split(text, "\n"); SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(); String line = null; for (int index = 0; index < lines.length; ++index) { line = lines[index]; int length = spannableStringBuilder.length(); spannableStringBuilder.append(line); if (index != lines.length - 1) { spannableStringBuilder.append("\n"); } else if (TextUtils.isEmpty(line)) { spannableStringBuilder.append("\u200B"); } spannableStringBuilder.setSpan(new BulletSpan(30), length, length + 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); } myEdit.setText(spannableStringBuilder); } 

Result: enter image description here

Ideally, I would make a custom EditText class that adds this character internally, but removes it when text is sent to any other object.

+9
source share

It's good?

 EditText myEdit = (EditText) this.findViewById(R.id.myedit); myEdit.setText("a\nb\n\n"); Spannable s = myEdit.getText(); s.setSpan(new BulletSpan(30), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE); s.setSpan(new BulletSpan(30), 2, 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE); s.setSpan(new BulletSpan(30), 4, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE); myEdit.setText(s); myEdit.setSelection(s.length()-1); 

Result

enter image description here

+2
source share

I have a simple solution, just add a space at the end of a new line

 EditText myEdit = (EditText) this.findViewById(R.id.myedit); myEdit.setText("a\nb\n "); //notice the space after newline Spannable s = myEdit.getText(); s.setSpan(new BulletSpan(30), 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); s.setSpan(new BulletSpan(30), 2, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); s.setSpan(new BulletSpan(30), 4, 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); myEdit.setText(s); 
+1
source share

All Articles