Question

I am developing an app which needs to align justification for substring of html string.

Issue: I have used webview to set justification for html text by using following code

String htmlText = "<html><body style=\"text-align:justify\"> %s </body></Html>";
WebView webView = new WebView(SubProducts.this);
webView.loadData(String.format(htmlText, description), "text/html", "utf-8");

but when I set the same idea for substring like this:

String htmlText = "<html><body style=\"text-align:justify\"> %s </body></Html>";
WebView webView = new WebView(SubProducts.this);
webView.loadData(String.format(htmlText, description.substring(1,170)), "text/html", "utf-8");

in my output text html text(

<html><body style=\"text-align:justify\"> %s </body></Html>) 

is also visible.How to achieve this.Could anybody help me? Thanks in advance.

Was it helpful?

Solution 2

This solved my issue:

Initially my description contains HTML tags so i did with following idea:

WebView webView = new WebView(SubProducts.this);
Spanned sub=Html.fromHtml(description);
String s = "<html><head><style type='text/css'>body {margin:0px;color:707070;"
        + "text-align: justify;}</style></head><body>"
        + sub.subSequence(1, 150)
        + "</body></html>";

webView.loadDataWithBaseURL("", s, "text/html", "utf-8", null);

OTHER TIPS

Find Substring and load inside WebView , that works fine for me. For justification ready-made code looks below

    public static void loadHtmlContentForAboutUs(WebView view, String appendText) {
    String s = "<html><head><style type='text/css'>body {margin:0px;color:707070;"
            + "text-align: justify;}</style></head><body>"
            + appendText
            + "</body></html>";

    view.loadDataWithBaseURL("", s, "text/html", "utf-8", null);
    view.setBackgroundColor(Color.TRANSPARENT);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top