2

in my android application I have to load PDFs in the server into a webview. according to a stack-overflow question I used Google DOC view in loading it. but my problem is some times it does not show the file in the webview while in another instance it displays quite well. I can't figure out the problem. but the code appears to be OK. my code segment is as below.

WebViewLable.getSettings().setJavaScriptEnabled(true);    
    WebViewLable.getSettings().setLoadWithOverviewMode(true);
    WebViewLable.getSettings().setUseWideViewPort(true);  

    fullURL=GoogleDOCview+"https://livefarmer.com/labels/"+itemURL;
    WebViewLable.loadUrl(fullURL);

}

private class Callback extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(
            WebView view, String url) {
        return(false);
    }
}

and my webview is as below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/app_bg_black"
    android:orientation="vertical" >

    <include
        android:id="@+id/nav_bar"
        android:layout_width="fill_parent"
        layout="@layout/nav_bar" />


    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webViewLable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:windowSoftInputMode="adjustResize" />

</LinearLayout>

this is the logcat output when it not showing the pdf.

enter image description here

this is the logcat output when it showing the pdf in the webview.

enter image description here

So whats the wrong with my code. how can I fix this. Thanks and regards!

Samantha Withanage
  • 3,811
  • 10
  • 30
  • 59
  • What is `WebViewLable`? Is this even real code? What is `GoogleDOCview`, and what is `itemURL`? – jww Aug 27 '14 at 09:39
  • yep. this is a real code. from which I'm working on now. So I can't post whole code as it's not belongs to me. **WebViewLable** is my webview. the PDFs I'm loading are taking from a server. and **itemURL** is the URL of those and **GoogleDOCview** is Google Document Viewer. – Samantha Withanage Aug 27 '14 at 09:49
  • there no any change in Logcat while pdf is showing or not.. – Vaishali Sutariya Aug 27 '14 at 10:17

2 Answers2

2
String PdfUrl = "http://pdfexample.com/abcd.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + PdfUrl;
Log.i(TAG, "PDF URL : " + url);
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl(url);

try this

Vaishali Sutariya
  • 5,093
  • 30
  • 32
0

The way I have managed to resolve the above is by checking the content height after the WebView has finished loading and if its value is 0, it then attempts to reload the url.

        myWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (view.getContentHeight() == 0) view.loadUrl(url);
            }
        });
ChrisZ
  • 81
  • 9