How to Add Bulleted List to Android Application

How to add bulleted list to android application?

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

• foo<br/>
• bar<br/>
• baz<br/>

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));

Add Bullets with proper formatting in Android

Would You be satisfied of this example?

public class MainActivity extends AppCompatActivity {

private TextView tvProdDesc;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvProdDesc = (TextView) findViewById(R.id.text1);

String longDescription = "Enhanced bass performance.\n" +
"Lightweight headband enhances comfort and adds durability\n" +
"Easy to adjust headband ensures optimum fit and comfort\n" +
"2 metre-long cable";

String arr[] = longDescription.split("\n");

int bulletGap = (int) dp(10);

SpannableStringBuilder ssb = new SpannableStringBuilder();
for (int i = 0; i < arr.length; i++) {
String line = arr[i];
SpannableString ss = new SpannableString(line);
ss.setSpan(new BulletSpan(bulletGap), 0, line.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.append(ss);

//avoid last "\n"
if(i+1<arr.length)
ssb.append("\n");

}

tvProdDesc.setText(ssb);
}

private float dp(int dp) {
return getResources().getDisplayMetrics().density * dp;
}
}

Result:

Sample Image

How do I add a bullet symbol in TextView?

You have to use the right character encoding to accomplish this effect. You could try with


Update

Just to clarify: use `setText("\u2022 Bullet");` to add the bullet programmatically. `0x2022 = 8226`

How to create bulleted text list in Android Jetpack compose?

Found it while brainstorming.
Just another approach with annotated string and only one Text.

val bullet = "\u2022"
val messages = listOf(
"Hey This is first paragraph",
"Hey this is my second paragraph. Any this is 2nd line.",
"Hey this is 3rd paragraph."
)
val paragraphStyle = ParagraphStyle(textIndent = TextIndent(restLine = 12.sp))
Text(
buildAnnotatedString {
messages.forEach {
withStyle(style = paragraphStyle) {
append(bullet)
append("\t\t")
append(it)
}
}
}
)

update: screenshot of output
Sample Image

Add bullets to Android's listview

I just made a custom listview, couldn't find any other way.

Android: using Spannable for putting text with bullets into TextView

I got so sick of dealing with bulleted text that I wrote a TextView subclass I call BulletTextView.

I have texts in the resource file, as you do. I formatted all the texts to use the Unicode bullet character \u2022 to mark the bullets. So a sample text might look like this:

<string name="product_description_text">Our product is absolutely amazing, because 
it has these features:
\n\n\u2022 First awesome feature
\n\u2022 Second awesome feature
\n\u2022 Third awesome feature
\n\n(Note that users with a free trial license can\'t access these features.)\n</string>

BulletTextView overrides TextView.setText() to scan the text for the bullet characters, remove them and save the positions to mark the bulleted spans:

@Override
public void setText(CharSequence text, BufferType type) {

StringBuilder sb = new StringBuilder();
List<Integer> markers = new ArrayList<Integer>();

for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);

switch (ch) {

case '\u2022':

// we found a bullet, mark the start of bullet span but don't append the bullet char
markers.add(sb.length());

// ... I do some other stuff here to skip whitespace etc.
break;

case '\n':

// we found a newline char, mark the end of the bullet span
sb.append(ch);
markers.add(sb.length());

// ... I do some stuff here to weed out the newlines without matching bullets

break;

// ... I have some special treatment for some other characters,
// for instance, a tab \t means a newline within the span

default:
// any other character just add it to the string
sb.append(ch);
break;
}
}

// ... at the end of the loop I have some code to check for an unclosed span

// create the spannable to put in the TextView
SpannableString spannableString = new SpannableString(sb.toString());

// go through the markers two at a time and set the spans
for (int i = 0; i < markers.size(); i += 2) {
int start = markers.get(i);
int end = markers.get(i+1);
spannableString.setSpan(new BulletSpan(gapWidth), start, end, Spannable.SPAN_PARAGRAPH);
}

super.setText(spannableString, BufferType.SPANNABLE);
}

I left out some code that was specific to my application, but this is the basic framework for solving your problem.

Not sure about making your bullet a different color, but there is a BulletSpan constructor public BulletSpan(int gapWidth, int color) that may do the trick.

I tried to figure out how to use LineHeight to make larger lines to separate the bullet paragraphs, but I couldn't make it work. I just use a newline to separate the two bullet sections.



Related Topics



Leave a reply



Submit