Florian Schaal
Florian Schaal

Reputation: 2660

Loading url with pdf in monodroid webview

Im trying to load an url in a webview. The url links to a pdf file on the website. I want to display this pdf file in the webview. For some reason I only see a white screen and the pdf is not loading.

Here is code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Webkit;

namespace TrackandTrace.Droid
{
    [Activity (Label = "PodActivity")]          
    public class PodActivity : Activity
    {
        public string PodUrl { get; set; }


        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.PodLayout);

            var PodWebView = FindViewById<WebView> (Resource.Id.webViewPod);
            PodUrl = Intent.GetStringExtra ("PodUrlString" ?? "No Pod Data available");

            PodWebView.Settings.AllowFileAccess = true;
            PodWebView.Settings.JavaScriptEnabled = true;
            PodWebView.Settings.BuiltInZoomControls = true;
            PodWebView.LoadData (PodUrl);

            PodWebView.SetWebViewClient (new PodWebViewClient ());

            // Create your application here
        }

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

Upvotes: 5

Views: 2435

Answers (3)

Younes
Younes

Reputation: 46

you have to add this link to your pdf link or just tape the same code below

WebView webview = FindViewById<WebView>(Resource.Id.webView1);
        urlPdf = ("example of url");
        WebSettings settings = webview.Settings;
        settings.JavaScriptEnabled = true;
        webview.LoadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + urlPdf);

and it's important to add this link before urlPDF "http://drive.google.com/viewerng/viewer?embedded=true&url="

Upvotes: 0

Florian Schaal
Florian Schaal

Reputation: 2660

I found a way that fits my appilication and still opens pdf's in de "webView" Here is the code:

PodWebView.LoadUrl ("http://docs.google.com/viewer?url=" + PodUrl);

This is not as smooth as in IOS webview but this way you will be able to open it regardless what device you have.

Upvotes: 2

Cheesebaron
Cheesebaron

Reputation: 24460

I don't think it is possible to load a PDF inside a WebView. At least that is the impression I get from reading other SO questions about the same issue.

I would personally use the default PDF reader instead like so:

var intent = new Intent(Intent.ActionView);
intent.SetDataAndType(uri, "application/pdf");
intent.SetFlags(ActivityFlags.ClearTop);
StartActivity(intent);

Upvotes: 1

Related Questions