Bullets List in Android?

 

A much needed, sometimes hidden in the framework, and mimicked with unnecessary WebViews and sort of inner HTML injections to xml resources – a bullet list is much easier to implement then you would expect.

When taking the easy solution – using WebViews , you are making the UI hierarchy much heavier then it should be, and when your UI is already rich with layouts and widgets, it seems bad to overload it with heavy WebViews.

The Android.text framework gives us – the developers an easy to use API to build special text spans,

in the next code snippet we’ll see how a bullet list can be implemented:

let’s say we have a simple TextView which we need to hold a text with a bullet at the left hand of it:

TextView mTv = (TextView)findViewById(R.id.tv_1);

CharSequence cs = getText(R.String.sample_text);

SpannableString ss = new SpannableString(cs);

ss.setSpan(new BulletSpan(15), 0, cs.length(), 0);

mTv.setText(ss);

 

Explanation:

1. Get a hook on a TextView

2. Construct a CharSequence

3. Construct a SpannableString with cs.

4. Set the span on the SpannableString to be a BulletSpan with a margin of 15, starts at 0, ends at the

    end of the String and without any special flags.

5. Set the text of the TextView to be the spannableText which already includes the bullet span.

all done

Comments

Popular posts from this blog

Spinner VS AutoCompleteTextView

How to update UI from a different thread

Utilizing Http response cache