Add Bullets with proper formatting in Android
Asked Answered
C

1

7

I wanted to show bullets in android text. I have added them successfully. I search over internet and found that you can add bullets. but if text goes more than one line it does not follow proper spacing like html list does.

See Screenshot below.

enter image description here

I have used following code to add bullets.

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");
        StringBuilder desc = new StringBuilder();
        for (String s : arr){
            desc.append("<li>"+s+"</li>");
        }
        String newDesc = "<ul>"+desc+"</ul>";

        tvProdDesc.setText(Html.fromHtml(newDesc, null, new UlTagHandler()));

Here is my

UlTagHandler.java

public class UlTagHandler implements Html.TagHandler {

    public void handleTag(boolean opening, String tag, Editable output,
                          XMLReader xmlReader) {
        if(tag.equals("ul") && !opening) output.append("\n");
        if(tag.equals("li") && opening) output.append("\n•\t");
    }
}

But I want text should be properly formatted like word processor does.

I want this type of output

enter image description here

Can we do anything simillar to above image?

Consol answered 6/12, 2016 at 8:20 Comment(5)
no it is not duplicate See I have added bullets. I wanted well formatted text.Consol
what minSdk do You provide?Finnic
minSdkVersion 16 and targetSdkVersion 23Consol
@AshishKudale coud You fix Your topic title? It is all about bullets. It is important to change because it will be more helpful for people looking for solution for this like You.Finnic
sure I 'll do it.Consol
F
16

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:

enter image description here

Finnic answered 6/12, 2016 at 9:0 Comment(1)
is this applicable to marshmallow version?When I used this code I am getting error java.lang.NoSuchMethodError: No direct method <init>(III)V in class Landroid/text/style/BulletSpan; or its super classes (declaration of 'android.text.style.BulletSpan' appears in /system/framework/framework.jar) in marshmallow version.Can anyone help me?Demineralize

© 2022 - 2024 — McMap. All rights reserved.