androidandroid-webviewtext-justify

Set justification to substring of html string in android


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.


Solution

  • 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);