cavallo
cavallo

Reputation: 4414

How to pass html string to webview on android?

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

Please suggest in my code, where I am going wrong and the correct way to get the formatted html strings on the web view.

@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.

Upvotes: 170

Views: 180017

Answers (5)

Talha Shahab
Talha Shahab

Reputation: 302

After struggling the 2 hours of effort I found the solution

I want to run this code in webview

"<div style='width:100%;height:0px;position:relative;padding-bottom:56.250%;background:#000;'><iframe src='https://www.scorebat.com/embed/v/N09DWllRdlNraERnSXRRa1VJMTF4Zz09/?token=MTkxMTdfMTY1MjEyMzY2MF9kNzRkZTI4MTc3ZDAxZTNkOTQyOTg1OWE3MTcyYmFhY2EyYTI2YTZh&utm_source=api&utm_medium=video&utm_campaign=apifd' frameborder='0' width='100%' height='100%' allowfullscreen allow='autoplay; fullscreen' style='width:100%;height:100%;position:absolute;left:0px;top:0px;overflow:hidden;'></iframe></div>"

But I only got the white screen after trying all the answers

Now do this as your code will also work

WebView webview = (WebView) findViewById(R.id.webview);
 webview.setWebViewClient(new WebViewClient());
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webview.getSettings().setPluginState(WebSettings.PluginState.ON);
        webview.getSettings().setMediaPlaybackRequiresUserGesture(false);
        webview.setWebChromeClient(new WebChromeClient());
        webview.loadDataWithBaseURL(null, data, "text/html", "UTF-8", null);

Here webview.loadDataWithBaseURL(null, data, "text/html", "UTF-8", null); data is the HTML and javascript code

Add this line in your application tag as well

android:hardwareAccelerated="true"

I hope it will solve your issue

Upvotes: 0

Siddhpura Amit
Siddhpura Amit

Reputation: 15128

i have successfully done by below line

 //data == html data which you want to load
 String data = "Your data which you want to load";

 WebView webview = (WebView)this.findViewById(R.id.webview);
 webview.getSettings().setJavaScriptEnabled(true);
 webview.loadData(data, "text/html; charset=utf-8", "UTF-8");

Or You can try

 webview.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);

Upvotes: 237

Arindam
Arindam

Reputation: 81

I was using some buttons with some events, converted image file coming from server. 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

Upvotes: 8

Arslan Anwar
Arslan Anwar

Reputation: 18746

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

Upvotes: 179

Jeffrey Neo
Jeffrey Neo

Reputation: 3809

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

Upvotes: 46

Related Questions