How to pass html string to webview on android

android webview
android webview load html code
open html string in webview
extracting html from a webview
android webview example
android render html without webview
android get html of webview
get text from webview android

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.

@Override
    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();
            settings.setUseWideViewPort(true);
            settings.setLoadWithOverviewMode(true);
            settings.setJavaScriptEnabled(true);
            settings.setDefaultZoom(ZoomDensity.FAR);
            // wv.setBackgroundColor(0);
            wv.setVerticalScrollBarEnabled(false);
            wv.setHorizontalScrollBarEnabled(false);
            /*String txtChapTitle = Intro.book.getsecretList().get(position)
                    .getChtitle().toString();*/

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

            StringBuilder content = new StringBuilder();
            content.append(chapterTitle+SubChapterTitle+chapterIntro+chapterContent);

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

            wv.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    view.setHapticFeedbackEnabled(false);
                }
            });

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

            wv.loadUrl("file:///android_asset/wordview.html");
        }
        return view;
    }
}

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


i have successfully done by below line

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

How to load html string in WebView in Android, Source code: Step 1 :Creating Project. Create an android application project named “WebViewLoadDataExampleApp”. Step 2 : Creating Layout. Change res ->layout -> activity_main.xml as below: <? Step 3 : Creating MainActivity. Change src/main/packageName/MainActivity.java as below: package com. Lets say You are creating an android app and you have some user agreement or some web pages hosted online, you can just render it using Android WebView. There are two methods which we generally use to open web url or load html data.


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

http://developer.android.com/reference/android/webkit/WebView.html

WebView load from String, the good way (the working way!), Use WebView#loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) instead. Note to myself: to type inline code, type 3 backtick (`), hit enter, then go back between the first and the second one, and the inline code is then took into account, delete the backtick remaining. 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.


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

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

2 How to load html string in webview in android, 2 How to load html string in webview in android | webview in android studio Full webview Duration: 2:42 Posted: May 27, 2019 Summary. So, while you can render an HTML string in an Android TextView, the question becomes, “Should you do this?”For all but the simplest cases, my guess is that you’ll want to use a WebView instead of a TextView, as demonstrated by this example.


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

How to pass html string to webview on android, i have successfully done by below line //data == html data which you want to load WebView webview = (WebView)this. Quick search to how to do html text in a WebView, you find everywhere to do it like this: myWebView.load(myHtmlString, "text/html", "utf-8") It is working as long as you load it only once! 😱 As I am willing to change the text according to an event, I was stuck to understand why the WebView never refreshes its data.


How to pass html string to webview on android?, The "text/html; charset=utf-8" makes a big difference, symbol characters don't render properly without it. To load your data in WebView. Call loadData() method of� Unfortunately there is not easy way to do this. See How do I get the web page contents from a WebView?. You could just make a HttpRequest to the same page as your WebView and get the response.


4. WebView, WebKit, and WebSettings, Selection from Building Hybrid Android Apps with Java and JavaScript [Book] you can request it to load a web page by using the loadURL() API passing the You can request the WebView to render any valid HTML as a string using the� This is the best answer. I was trying to pass a 5MB JSON string to the WebView, and loadUrl wasn't capable of doing so. evaluateJavascript, on the other hand, had no issues with this. – Daniel F Dec 3 '19 at 8:55


WebView, public class WebView extends String � SCHEME_MAILTO Enables debugging of web contents (HTML / CSS / JavaScript) loaded into any WebViews of this� Also, since you cannot change the contents of your asset folder during runtime you should load the html file contents into a String variable and use the replace method to replace the token with a real URL and, finally, pass that string to your webview.