Ordered Lists Inside An Android Textview
Get hold of your view, and use it like this:
SpannableStringBuildercontent=newSpannableStringBuilder();
for(String text : list) {
intcontentStart= content.length();
content.append(text);
content.setSpan(newNumberIndentSpan(15, 15, number), contentStart, content.length(), 0);
}
TextViewview= findViewById(R.id.....);
view.setText(content);
Solution 2:
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 agapwidthSo you won't need anymore to append the bullet which is mentioned in the answer by @jk2K,
String leadingString = number + ". ";
content.append(leadingString);
Solution 3:
Go to res/values/strings.xml then paste below code
<stringname="list"><li>1) Item 1</li>\n <li>2) Item 2</li>\n <li>3) Item 3</li>\n </string>Then go to your layout file which contains TextView and replace with below code
<TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/list" />
Solution 4:
You can use this way instead:
• foo<br/>• bar<br/>• baz<br/>Solution 5:
Here is a solution I use. You can copy and paste it into an activity to see how it works, but you should change all attributes with variables for production. You can play with the padding parameters to indent it according to your needs. Instead of digits, you can use the bullet char if you want bullet list.
<LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><TextViewandroid:id="@+id/bullet1"android:textStyle="bold"android:layout_width="30dp"android:gravity="right"android:layout_height="wrap_content"android:paddingRight="5dp"android:text="1"android:textSize="20dp" /><TextViewandroid:id="@+id/bullet1Text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingBottom="10dp"android:text="First bullet. First bullet. First bullet. First bullet. First bullet. First bullet. First bullet. First bullet. "android:textSize="15dp" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><TextViewandroid:id="@+id/bullet2"android:textStyle="bold"android:layout_width="30dp"android:gravity="right"android:layout_height="wrap_content"android:paddingRight="5dp"android:text="2"android:textSize="20dp" /><TextViewandroid:id="@+id/bullet2Text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingBottom="10dp"android:text="Second bullet. Second bullet. Second bullet. Second bullet. Second bullet. Second bullet. Second bullet. "android:textSize="15dp" /></LinearLayout>
Post a Comment for "Ordered Lists Inside An Android Textview"