2

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.

Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49
Neeha
  • 727
  • 3
  • 9
  • 21

2 Answers2

1

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);
}
Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
  • thanq sameer..but again it is doing same thing html text is visible in webview. – Neeha Apr 16 '13 at 07:05
  • "description": "

    \r\n\tthis is a sample textview . which shows complete details about my product.

    \r\n", I need to show substring of this in justified format.
    – Neeha Apr 16 '13 at 07:13
  • Description include HTML tags? you are not able to explain your issue i think. Are you using TextView or WebView? And give a vote to answer if you want to help the person helping you – Tofeeq Ahmad Apr 16 '13 at 08:02
  • @Sameer..yes my description includes html tags & iam using webview. – Neeha Apr 16 '13 at 09:20
1

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);
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Neeha
  • 727
  • 3
  • 9
  • 21