How to add bulleted list to android application?

后端 未结 17 1296
抹茶落季
抹茶落季 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 20:02

    The two options you have for doing a bulleted list are

    • create the list using html (ul,ol) and load the html into a WebView
    • Load the data into a ListView and set the left drawable of your text view in the list item layout, to a suitable image for the bullet.

    Option 1 is the easiest.

    0 讨论(0)
  • 2020-11-29 20:03

    If You want to create bullet list with editText structure.

    I benefited this references

    You can use this bullets

               EditText  edtNoteContent = findViewById(R.id.editText_description_note);            
    
            edtNoteContent.addTextChangedListener(new TextWatcher(){
                @Override
                public void afterTextChanged(Editable e) {
    
                }
                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    
                }
                @Override
                public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter)
                {
                    if (lengthAfter > lengthBefore) {
                        if (text.toString().length() == 1) {
                            text = "◎ " + text;
                            edtNoteContent.setText(text);
                            edtNoteContent.setSelection(edtNoteContent.getText().length());
                        }
                        if (text.toString().endsWith("\n")) {
                            text = text.toString().replace("\n", "\n◎ ");
                            text = text.toString().replace("◎ ◎", "◎");
                            edtNoteContent.setText(text);
                            edtNoteContent.setSelection(edtNoteContent.getText().length());
                        }
                    }
                }
            });
    
    0 讨论(0)
  • 2020-11-29 20:06

    use simple TextView with a compound drawable. For example

    <TextView     
        android:text="Sample text"
        android:drawableLeft="@drawable/bulletimage" >
    </TextView>
    
    0 讨论(0)
  • 2020-11-29 20:07

    Went completely overkill and made a custom text view.

    Use it like this:

    <com.blundell.BulletTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="--bullet 1 --bullet two --bullet three --bullet four" />
    

    and the code:

    package com.blundell;
    
    import android.content.Context;
    import android.text.Html;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    public class BulletTextView extends TextView {
        private static final String SPLITTER_CHAR = "--";
        private static final String NEWLINE_CHAR = "<br/>";
        private static final String HTML_BULLETPOINT = "&#8226;";
    
        public BulletTextView(Context context, AttributeSet attrs) {
            this(context, attrs, android.R.attr.textViewStyle);
        }
    
        public BulletTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            checkForBulletPointSplitter();
        }
    
        private void checkForBulletPointSplitter() {
            String text = (String) getText();
            if (text.contains(SPLITTER_CHAR)) {
                injectBulletPoints(text);
            }
        }
    
        private void injectBulletPoints(String text) {
            String newLinedText = addNewLinesBetweenBullets(text);
            String htmlBulletText = addBulletPoints(newLinedText);
            setText(Html.fromHtml(htmlBulletText));
        }
    
        private String addNewLinesBetweenBullets(String text) {
            String newLinedText = text.replace(SPLITTER_CHAR, NEWLINE_CHAR + SPLITTER_CHAR);
            newLinedText = newLinedText.replaceFirst(NEWLINE_CHAR, "");
            return newLinedText;
        }
    
        private String addBulletPoints(String newLinedText) {
            return newLinedText.replace(SPLITTER_CHAR, HTML_BULLETPOINT);
        }
    
    }
    
    0 讨论(0)
  • 2020-11-29 20:07

    For single line text you can simply use drawables:

    <TextView
        android:id="@+id/txtData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableStart="@drawable/draw_bullet_list"
        android:drawablePadding="@dimen/padding_8dp"
        android:text="Hello"
        android:textColor="@color/colorBlack" />
    

    draw_bullet_list.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
        <solid android:color="@color/colorAccent" />
    
        <size
            android:width="12dp"
            android:height="12dp" />
    
    </shape>
    

    You can change shape, size,color based on your requirement.

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