Display a document from Google Docs in WebView

Problem displaying Google documents in WebView (Android)

this is my code

mWebView=(WebView)findViewById(R.id.web);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("https://docs.google.com/viewer?url="+ "https://docs.google.com/file/d/0B7pKTkDz8c3gWGNRTWJidTBTVmc/edit?usp=sharing");

When I click here, my Android shows options to open a link (e.g. Browser, Chrome, Drive, Internet) and opens the link in a pop-up Browser!

+9
source share
1 answer

How to get Android WebView to handle redirects

Google redirects you to a different URL, and WebViewallows the OS to handle redirection.

Use this code WebViewfor internal process redirection:

    // By default, redirects cause jump from WebView to default
    // system browser. Overriding url loading allows the WebView  
    // to load the redirect into this screen.
    mWebView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }
    });

By the way, you can do many other interesting things in this function, such as custom processing of various URLs.

+13

All Articles