How to apply Bullet and Numbered List in android Edit-text
Asked Answered
F

0

7

I am trying to apply Bulleted list and Numbering list in android edit-text when user press button. For that i have tried below code.

For 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 above code i am able to add Bullet and Number at particular position but the problem is, i want to implement functionality like, when i press enter key it should automatically add Bullet to next line and again if i press Enter key without entering any text it should remove that Bullet, if i enter some text on second line and then if i press enter key, it should add Bullet to next line also. Similar kind of functionality like this application. https://play.google.com/store/apps/details?id=com.hly.notes

Same for number listing also, it is always adding 1) it is not incrementing the value, and for number listing also i want to implement similar king of functionality like bullet, when i apply number list and when i press enter it should automatically add 2) to next line.

Anyone have any idea how to achieve this. Thank you in advance.

Flannel answered 12/1, 2015 at 5:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.