Home » Android » How to pass html string to webview on android

How to pass html string to webview on android

Posted by: admin March 11, 2020 Leave a comment


Hi I am parsing xml and then loading it to web view, after parsing I am creating four strings so that I could append all string to one view. I am able to get two views on the web view but not the first two strings.

Pls suggest me with my code, where am I going wrong and what is the correct way to get the formatted html strings on the web view. Pls have a look at my code and help me solve this issue.

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        String chapterTitle = "";
        String SubChapterTitle="";
        String chapterIntro ="";
        String chapterContent="";
        View view = convertView;
        if (convertView == null) {
            // view = inflater.inflate(resourceid, null);
            view = getLayoutInflater().inflate(R.layout.webviewitem, null);
        synchronized (view) {
            WebView wv = (WebView) view.findViewById(R.id.contentWebView);

            WebSettings settings = wv.getSettings();
            // wv.setBackgroundColor(0);
            /*String txtChapTitle = Intro.book.getsecretList().get(position)

            if (!(Intro.book.getsecretList().get(position).getChtitle()
            chapterTitle = "<b><fontSize=4>"+Intro.book.getsecretList().get(position)
            if (!(Intro.book.getsecretList().get(position)
                    .getSubtitle() == null)) {
                SubChapterTitle = "<b><fontSize=4>"+Intro.book.getsecretList().get(position)
            if (!(Intro.book.getsecretList().get(position)
                    .getIntro() == null)) {
            chapterIntro = "<b><fontSize=2>"+Intro.book.getsecretList().get(position)
            if (!(Intro.book.getsecretList().get(position)
                    .getContent() == null)) {
            chapterContent = "<fontSize=2>"+Intro.book.getsecretList().get(position)

            StringBuilder content = new StringBuilder();

            JsInterface Jsi = new JsInterface();
            Jsi.wordDef = content ;
            Log.v("Content", "" +content);
            wv.addJavascriptInterface(Jsi, "interfaces");

            wv.setWebViewClient(new WebViewClient() {
                public void onPageFinished(WebView view, String url) {

            wv.setWebChromeClient(new WebChromeClient() {
                public boolean onJsAlert(WebView view, String url,
                        String message, JsResult result) {
                    return super.onJsAlert(view, url, message, result);

        return view;

I am able to get chapterIntro and chaptercontent on the web view but not the first two strings pls help me friends.

How to&Answers:

i have successfully done by below line

 //data == html data which you want to load 
 WebView webview = (WebView)this.findViewById(R.id.webview);
 webview.loadDataWithBaseURL("", data, "text/html", "UTF-8", "");


To load your data in WebView. Call loadData() method of WebView

webView.loadData(yourData, "text/html; charset=utf-8", "UTF-8");

You can check this example



Passing null would be better. The full codes is like:

WebView wv = (WebView)this.findViewById(R.id.myWebView);
wv.loadDataWithBaseURL(null, "<html>...</html>", "text/html", "utf-8", null);


Loading normal data wasn’t working for me, converting into Base64 working just fine.

String unencodedHtml ="<html><body>'%28' is the code for '('</body></html>";
tring encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(), Base64.NO_PADDING);
webView.loadData(encodedHtml, "text/html", "base64");

Find details on WebView