I want to display an ordered list inside a TextView, for example:
1) item 1
2) item 2
Using the following layout:
I followed the answer by answer by @jk2K and made modifications to his code. As I need to have indentation for each bullet points,
String[] textArray = {
"dfsdljjlfsdsdfjsdjldssdfidfsjdljasdfjfds\n",
"sdfjdfjlkfdjdfkfjiwejojodljfldsjodsjfsdjdlf\n",
"djsdfjsdffjdflljfjsadfdjfldfjl"
};
SpannableStringBuilder content = new SpannableStringBuilder();
int number = 1;
for (String t1 : textArray) {
int contentStart = content.length();
content.append(t1);
int contentEnd = content.length();
content.setSpan(
new BulletSpan(10),
contentStart,
contentEnd,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE
);
number++;
}
I used the BulletSpan class from android library and replaced new LeadingMarginSpan.Standard(0, 66)
with new BulletSpan(10)
which creates a BulletSpan with width. As mentioned in the BulletSpan class documentation
BulletSpan(int gapWidth)
Creates a BulletSpan based on a gap width
So you won't need anymore to append the bullet which is mentioned in the answer by @jk2K,
String leadingString = number + ". ";
content.append(leadingString);