Wednesday, 24 October 2018

Android WebView: Display Website with PDF

In my Xamarin.Android App I am using a WebView to display a website:

AXML:

<LinearLayout
        android:id="@+id/auditStructurUseCaseLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        <WebView
            android:id="@+id/auditWebview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

Fragment.cs:

    _webview.SetWebViewClient(new ExtendWebViewClient());
    WebSettings webSettings = _webview.Settings;
    webSettings.JavaScriptEnabled = true;
    webSettings.DisplayZoomControls = false;
    webSettings.BuiltInZoomControls = true;
    _webview.LoadUrl(URL);

ExtendWebViewClient:

internal class ExtendWebViewClient : WebViewClient
{
    public override bool ShouldOverrideUrlLoading(WebView view, string url)
    {
        view.LoadUrl(url);
        return true;
    }
}

This works well for normal websites. I can navigate inside awebsite and can use it like a normal browser.

Problem: I want to display PDFs which are embedded inside a website. Today's browser come with PDF Plugins to display PDF, so the question is, is there a kind of PDF Plugin for the WebView or am I missing some settings to display a PDF inside a Website?

If I try to open websites with PDF, the website asks for a PDF-Viewer.

Translation: Unfortunately, the PDF can not be displayed because you do not have a PDF viewer. You can download the PDF here. enter image description here

Unfortunately, the download-button is functionless aswell.

Or maybe someone has another approach which does not use a WebView.

EDIT:

I tried using _webview.SetWebChromeClient(new WebChromeClient()); but this opens the the Chrome App. So I tried to use WebViewClient and ChromeViewClient like suggested in this question.

WebSettings webSettings = _webview.Settings;
            webSettings.JavaScriptEnabled = true;
            webSettings.DisplayZoomControls = false;
            webSettings.BuiltInZoomControls = true;
            webSettings.SetSupportMultipleWindows(true);
            _webview.SetWebChromeClient(new WebChromeClient());
            _webview.SetWebViewClient(new ExtendWebViewClient());

But this leads to the same problem as described.



from Android WebView: Display Website with PDF

No comments:

Post a Comment