browep explained nice the way over HTML. The provided solution with the html entity can be useful. But it includes only the bullet. If your text wraps, the indent will not be correct.
I found other solutions embedding a web view. That maybe is appropriate for some, but i think its kind of overkill... (The same with using a list view.)
I like the creative approach of Nelson :D, but it does not give you the possibility of adding an unordered list to a text view.
My example of an unordered list with bullets using BulletSpan
I have found an alternate.. just copy this bullet " • " (it is a text) and paste in your text view's text, you can change the bullet color by changing the textcolor and as well all other attributes like size, height width... :)
you can use short-cut to get this bullet while typing
I find this to be the easiest way, leave the textView as it is in the xml file and use the following java code. it worked perfectly fine for me.
private static final String BULLET_SYMBOL = "•";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial);
TextView tv = (TextView) findViewById(R.id.yourTextView);
tv.setText("To perform this exercise you will need the following: "
+ System.getProperty("line.separator")//this takes you to the next Line
+ System.getProperty("line.separator")
+ Html.fromHtml(BULLET_SYMBOL + " Bed")
+ System.getProperty("line.separator")
+ Html.fromHtml(BULLET_SYMBOL + " Pillow"));
}
Inspired by the various answers here, I created a Utility class to make this an easy one liner. This will create a bulleted list with indentation for wrapped text.
It has methods for combining strings, string resources and string array resources.
It will create a CharSequence which you can pass to a TextView. For example:
CharSequence bulletedList = BulletListUtil.makeBulletList("First line", "Second line", "Really long third line that will wrap and indent properly.");
textView.setText(bulletedList);
Hope it's helpful. Enjoy.
Note: This will use the system standard bullet, a small circle the same color as the text. If you want a custom bullet, consider subclassing BulletSpan and overriding its drawLeadingMargin() to draw the bullet you want. Take a look at the BulletSpan source for an idea how it works.
public class BulletTextUtil {
/**
* Returns a CharSequence containing a bulleted and properly indented list.
*
* @param leadingMargin In pixels, the space between the left edge of the bullet and the left edge of the text.
* @param context
* @param stringArrayResId A resource id pointing to a string array. Each string will be a separate line/bullet-point.
* @return
*/
public static CharSequence makeBulletListFromStringArrayResource(int leadingMargin, Context context, int stringArrayResId) {
return makeBulletList(leadingMargin, context.getResources().getStringArray(stringArrayResId));
}
/**
* Returns a CharSequence containing a bulleted and properly indented list.
*
* @param leadingMargin In pixels, the space between the left edge of the bullet and the left edge of the text.
* @param context
* @param linesResIds An array of string resource ids. Each string will be a separate line/bullet-point.
* @return
*/
public static CharSequence makeBulletListFromStringResources(int leadingMargin, Context context, int... linesResIds) {
int len = linesResIds.length;
CharSequence[] cslines = new CharSequence[len];
for (int i = 0; i < len; i++) {
cslines[i] = context.getString(linesResIds[i]);
}
return makeBulletList(leadingMargin, cslines);
}
/**
* Returns a CharSequence containing a bulleted and properly indented list.
*
* @param leadingMargin In pixels, the space between the left edge of the bullet and the left edge of the text.
* @param lines An array of CharSequences. Each CharSequences will be a separate line/bullet-point.
* @return
*/
public static CharSequence makeBulletList(int leadingMargin, CharSequence... lines) {
SpannableStringBuilder sb = new SpannableStringBuilder();
for (int i = 0; i < lines.length; i++) {
CharSequence line = lines[i] + (i < lines.length-1 ? "\n" : "");
Spannable spannable = new SpannableString(line);
spannable.setSpan(new BulletSpan(leadingMargin), 0, spannable.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
sb.append(spannable);
}
return sb;
}
}
Here's another solution, not exactly adding a list to one textview, but I guess the goal is the same. It's using TableLayout, that needs only XML and it's really simple for small ordered or unordered lists. Below, sample code I used for this, not a line of code in Java.
Positive:
you can put whatever you like in table rows, doesn't have to be a textview
you can use it to create bulleted and numbered list or whatever
you can define indent using padding or layout_weight
Negative:
tedious for very long lists (unless you use some crafty text editor with regex)
every list item is store as a separate string resource
<string name="bullet_ed_list">\n\u2022 He has been Chairman of CFL Manufacturers Committee of ELCOMA, the All India Association of Lighting Equipment Manufacturers.
\n\u2022 He has been the President of Federation of Industries of India (FII).</string>