I have googled my question but there is not working answer provided. How do add a bulleted list to my textview.
The two options you have for doing a bulleted list are
Option 1 is the easiest.
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());
}
}
}
});
use simple TextView with a compound drawable. For example
<TextView
android:text="Sample text"
android:drawableLeft="@drawable/bulletimage" >
</TextView>
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 = "•";
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);
}
}
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.