1

I have String which contains HTML data with <ol> and <ul> tag. How to set both in TextView. I am able to set Bullets but not number format. What I did is here:

String html_title = demoList.get(position - 1).getText();
String li = html_title.replace("<li>", " \u25CF");
String mal = li.replace("</li>", "<br/>");
String main_hrml = mal.replace("\t", "\n").replace("\n\n", "\n").trim();

setTextViewHTML(((DemoViewHolderSecond) holder).demoSecondItemInitView.type_1_bodyTxt, main_html);


private void setTextViewHTML(TextView type_1_bodyTxt, String main_html) {
    CharSequence sequence = Html.fromHtml(main_html);
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
    for (URLSpan span : urls) {
        makeLinkClickable(strBuilder, span);
    }

    if (main_html.contains("<p>")){
        if (!main_html.contains("<ul")) {
            GetDeviceResolution getDeviceResolution = new GetDeviceResolution((CoverDetailsActivity) context);
            type_1_bodyTxt.setPadding(0, 0, 0, -(int) (getDeviceResolution.setHeight(0.06)));
        }else{
            GetDeviceResolution getDeviceResolution = new GetDeviceResolution((CoverDetailsActivity) context);
            type_1_bodyTxt.setPadding(0, 0, 0, -(int) (getDeviceResolution.setHeight(0.02)));
        }
    }
    type_1_bodyTxt.setText(strBuilder);
    type_1_bodyTxt.setLinkTextColor(Color.RED);

    removeLine(type_1_bodyTxt, main_html);
    type_1_bodyTxt.setMovementMethod(LinkMovementMethod.getInstance());
}
Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
coder_baba
  • 447
  • 3
  • 21

1 Answers1

-2

Try changing your code as stated below:

Add this method to your class or in any singleton class

public class UlTagHandler implements Html.TagHandler{
    @Override
    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" +Html.fromHtml("&#8226;"));
    }
}

Now setText to your TextView or EditText element

 String your_html_string= "<ul>\n" +
            "  <li>Coffee</li>\n" +
            "  <li>Tea</li>\n" +
            "  <li>Milk</li>\n" +
            "</ul>";

btnSubmit.setText(Html.fromHtml(your_html_string, null, new UlTagHandler()));

The above code is for tag only. For tag you can update yourself :D

Rest you need to do is to set property of your EditText or TextView element height to wrapContent do not assign some fixed height.

Ankush Bist
  • 1,862
  • 1
  • 15
  • 32