How to add bulleted list to android application?

后端 未结 17 1295
抹茶落季
抹茶落季 2020-11-29 19:14

I have googled my question but there is not working answer provided. How do add a bulleted list to my textview.

相关标签:
17条回答
  • 2020-11-29 19:44

    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 = "&#8226";
    
    @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"));
    }
    
    0 讨论(0)
  • 2020-11-29 19:45

    another way to support the missing HTML tags, is by replacing them nicely, as shown here

    0 讨论(0)
  • 2020-11-29 19:46

    Tough to do as ul/li/ol are not supported. Fortunately you can use this as syntactic sugar:

    &#8226; foo<br/>
    &#8226; bar<br/>
    &#8226; baz<br/>
    

    &#8226; is the html entity for a list bullet more choices are here http://www.elizabethcastro.com/html/extras/entities.html

    more about which tags are supported provided by Mark Murphy (@CommonsWare) http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html Load that up with Html.fromHtml

    ((TextView)findViewById(R.id.my_text_view)).setText(Html.fromHtml(myHtmlString));
    
    0 讨论(0)
  • 2020-11-29 19:47

    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;
    }
    
    }
    
    0 讨论(0)
  • 2020-11-29 19:48

    This is by far the easiest..

    <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>
    
    0 讨论(0)
  • 2020-11-29 19:50

    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

    for windows

    ALT + 7

    for mac

    ALT + 8

    0 讨论(0)
提交回复
热议问题