How to apply Bullet and numbered list in android Edit-text

I am trying to apply a Bulleted list and numbering in android edit text when a user button is clicked. For this, I tried under the code.

For a bullet

BulletSpan[] quoteSpan = str.getSpans(selectionStart, selectionEnd, BulletSpan.class); // If the selected text-part already has UNDERLINE style on it, then we need to disable it for (int i = 0; i < quoteSpan.length; i++) { str.removeSpan(quoteSpan[i]); exists = true; } // Else we set UNDERLINE style on it if (!exists) { str.setSpan(new BulletSpan(10), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } break; 

and for numbering

 int number = 0; NumberIndentSpan[] quoteSpan1 = str.getSpans(selectionStart, selectionEnd, NumberIndentSpan.class); number ++; // If the selected text-part already has UNDERLINE style on it, then we need to disable it for (int i = 0; i < quoteSpan1.length; i++) { str.removeSpan(quoteSpan1[i]); exists = true; } if (!exists){ str.setSpan(new NumberIndentSpan(5, 15, number), selectionStart, selectionEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } break; 

NumberIndentSpan.java

 public class NumberIndentSpan implements LeadingMarginSpan { private final int gapWidth; private final int leadWidth; private final int index; public NumberIndentSpan(int leadGap, int gapWidth, int index) { this.leadWidth = leadGap; this.gapWidth = gapWidth; this.index = index; } public int getLeadingMargin(boolean first) { return leadWidth + gapWidth; } public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout l) { if (first) { Paint.Style orgStyle = p.getStyle(); p.setStyle(Paint.Style.FILL); float width = p.measureText("4."); c.drawText(index + ")", (leadWidth + x - width / 2) * dir, bottom - p.descent(), p); p.setStyle(orgStyle); } } } 

Through the above code, I can add Bullet and Number to a specific position, but the problem is that I want to implement functions such as when I press the enter key, it automatically adds Bullet to the next line and again if I press Enter without typing any text, it should delete this Bullet if I enter some text in the second line, and then if I press the enter key, it should also add Bullet to the next line. Similar functionality like this app. https://play.google.com/store/apps/details?id=com.hly.notes

The same is for listing numbers, it always adds 1) , it does not increase the value, and for listing numbers I also want to implement a similar functionality king like bullet, when I use the list of numbers and when I press enter, it should automatically add 2) to the next line.

Does anyone know how to achieve this. Thank you in advance.

+6
source share

Source: https://habr.com/ru/post/980857/


All Articles