Disable popup option in Android WebView for Google Drive PDF viewer

I’m trying to display PDF files in my Android app using WebView. I’m loading the documents with the Google Docs viewer API. The URL I’m using looks like this:

https://docs.google.com/viewer?embedded=true&url=MY_PDF_URL

The PDF loads fine, but there’s a problem. The viewer shows two buttons: ‘Magnify’ and ‘Open in new window’. I want to get rid of the ‘Open in new window’ button. Is there a way to hide or disable just that option?

I’ve looked through the API docs but couldn’t find anything about customizing the viewer interface. Has anyone figured out how to do this? Maybe there’s a special parameter I can add to the URL?

Any tips or workarounds would be super helpful. Thanks!

I’ve encountered a similar challenge in my own Android development work. While there’s no official way to disable that ‘Open in new window’ button, I found a workaround that might help you. Instead of using the Google Docs viewer, I switched to using PDF.js, an open-source PDF viewer library. It gives you much more control over the UI elements displayed.

With PDF.js, you can create a custom interface without unwanted buttons. It takes a bit more setup initially, but it’s well-documented and offers greater flexibility. You’ll need to include the PDF.js files in your app’s assets and load them into the WebView. This approach also has the advantage of working offline once the PDF is loaded.

If you absolutely need to stick with the Google Docs viewer, you might try intercepting and blocking the ‘Open in new window’ action using WebView’s shouldOverrideUrlLoading method. It’s not perfect, but it could prevent users from opening PDFs externally.

I’ve dealt with this exact issue in a recent project. Unfortunately, there isn’t a direct method to remove the ‘Open in new window’ button through URL parameters or WebView settings, as Google provides very little customization for the viewer interface. Instead, I resolved it by injecting JavaScript into the WebView after the page loads. I set up a WebViewClient and used the onPageFinished callback to run a script that hides the button. Note that this approach might show the button briefly as the script executes, so you may need to adjust the timing or target the selector more precisely if the HTML structure changes.

hey there, i had the same prob b4. what worked 4 me was using mozilla’s pdf.js library instead. its way more customizable than google’s viewer. u can easily hide buttons u dont want. plus it works offline which is neat. might take a bit to set up but totally worth it imho