Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText: How to create an empty bullet paragraph by BulletSpan?

I use the same title with this question, because I think my question is very similar to that one, I read and tested the accepted answer very carefully, however the accepted answer doesn't work for me. Let me describe my question:

My code looks like:

 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 is:

  • a
  • b
  • [I want to see the 3rd bullet here, but it doesn't show up]

I tried Spannable.SPAN_INCLUSIVE_INCLUSIVE, Spannable.SPAN_INCLUSIVE_EXCLUSIVE, Spannable.SPAN_EXCLUSIVE_INCLUSIVE,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE, but none of these flags works 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:

  • a
  • b
  • c

I am working on a rich text editor, when user clicks bullet icon, I need to show an empty bullet, but now I am not sure what the problem might be, as I want to make a new empty BulletSpan (with only a dot, but no chars after it), but if there are no chars in the span's start and end, the dot doesn't show up.

like image 262
LiuWenbin_NO. Avatar asked Oct 16 '14 07:10

LiuWenbin_NO.


People also ask

How do you use a bullet span?

To construct a BulletSpan with a gap width of 40px, green bullet point and bullet radius of 20px: SpannableString string = new SpannableString("Text with\nBullet point"); string. setSpan(new BulletSpan(40, color, 20), 10, 22, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE);


2 Answers

It is an ugly solution, but I have not found any better - try adding an empty character in the end (something like zero-width space). This is producing the result you'd like (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);
    }

The result is: enter image description here

Ideally I'd make a custom EditText class which appends this character internally, but removes it when the text is being sent to any other object.

like image 147
Samuil Yanovski Avatar answered Sep 20 '22 09:09

Samuil Yanovski


Is this 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);

The result is

enter image description here

like image 34
krishnakumarp Avatar answered Sep 21 '22 09:09

krishnakumarp