Kaidul
Kaidul

Reputation: 15855

How to align contents in center into Webview

I am showing some contents into a webview which starts with a <p> and ends with </p> tag. I am loading the content into webview by loadDataWithBaseURL and trying to show the contents in middle:

public static String contentPrefix = "<html><head><style type='text/css'>#content_wrapper {position: relative; margin: 0px auto;} </style></head><div id='content_wrapper'>";
public static String contentPostfix = "</div></html>";
webView.loadDataWithBaseURL("", contentPrefix + result + contentPostfix, "text/html", "UTF-8", "");

But it is not working. What should I do to show that contents in the middle?

Upvotes: 0

Views: 1529

Answers (2)

Kaidul
Kaidul

Reputation: 15855

Finally it works!

    String desiredContent = "my test html";
    String strBody = "<html>"
        + "<head>"
        + "     <style type=\"text/css\">"
        + "     @font-face "
        + "         {   font-family: Tahoma;"
        + "             src:url(\"~/android_asset/fonts/MyriadPro-Regular.otf\") "
        + "         }"
        + "     #text"
        + "         {   font-family: Tahoma;"
        + "             font-size:14;"
        + "             text-align: center;"
        + "         }"
        + "     </style>"
        + ""
        + "</head>"
        + "<body dir=\"rtl\" id=\"text\">"
        + desiredContent 
        + " </body></html>  ";

myWebView.loadDataWithBaseURL(null, strBody, "text/html", "utf-8",
        null);

Upvotes: 2

Luciano Rodr&#237;guez
Luciano Rodr&#237;guez

Reputation: 2309

try:

public static String contentPrefix = "<html><head><style type=\"text/css\">p{text-align:center;}</head>";

Upvotes: 0

Related Questions