BulletSpan not working in Android Textview

2.1k views Asked by At

I'm trying to add bullets to the unordered lists using BulletSpan. But, I cannot find bullets in the list. Below is my code:

private static String appendBullet(int leadingMargin, String liText) {
    SpannableString spannableString = new SpannableString(liText);
    spannableString.setSpan(new BulletSpan(leadingMargin), 0, liText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    Log.d("Ul tags", String.valueOf(spannableString));
    return spannableString.toString();
}

Here liItext is the list item. I'm adding a BulletSpan here and then returning the item and adding it to the list and finally adding the entire list to the StringBuilder.

liList.add(appendBullet(15, liText));

List<String> liList = addListWithBullets(lists, new ArrayList<String>());

if (liList.size() > 0) {
    for (String str : liList) {
        builder.append(str);
        builder.append("<br/>");
    }
    Log.d("Ul tags", String.valueOf(liList));
}
1

There are 1 answers

0
Adil Hussain On

First of all you probably want to use SpannableStringBuilder instead of StringBuilder. Secondly, in your appendBullet(...) method by calling toString() on the SpannableString instance you're probably losing all of the spanning information! What you probably want to do instead is append each String which is to be bulleted to the SpannableStringBuilder instance and then call the setSpan(...) method on the SpannableStringBuilder instance for each of these Strings.

If it helps I've put together an extension function on the SpannableStringBuilder class which takes away some of this difficulty for you. You can find here.