How to have HTML New Line Feature in Android Text View?

前端 未结 6 1522
终归单人心
终归单人心 2021-01-01 12:38

I am using webservice in my android application. In my webservice result I get a HTML formatted String as result.

Assume this is my web service result:

相关标签:
6条回答
  • 2021-01-01 12:42
    String sBooks = "Books on Chennai:\n ....";
    

    Replace: \n with <br>:

    sBooks = sBooks.replace("\n", "<br>");
    

    Example using a TextView:

    myTextView.setText(Html.fromHtml(sBooks));
    
    0 讨论(0)
  • 2021-01-01 12:49

    The best option is instead of TextView use a WebView and

    final String mimeType = "text/html";
        final String encoding = "UTF-8";
        webView.loadDataWithBaseURL("", contentToShowString, mimeType,
                encoding, "");
    
    0 讨论(0)
  • 2021-01-01 12:54

    Just replace \n with <br/> tag and then pass the whole string to Html.fromHtml(String). By this it will be displayed in proper format.

    0 讨论(0)
  • 2021-01-01 12:56

    You should replace this \n with <br>. For more android HTML tags support go to Android Support HTML Tags or daniel-codes.blogspot.in

    0 讨论(0)
  • 2021-01-01 13:06

    Use this:

    b.setText(Html.fromHtml("<b>" + your string+ "</b>" + "<br/>" + cursor.getString(1)));
    
    0 讨论(0)
  • 2021-01-01 13:07

    have you tried with \r\n ?`

    If this not works then you can try with : \n with escape sequence : \\\n
    I hope this will help you :)

    Something like this

    string = string.replace("\\\n", System.getProperty("line.separator"));
    
    0 讨论(0)
提交回复
热议问题