How to unable download in webview android studio ?

 Disabling Downloads in Android WebView


By default, Android WebView allows users to download files from the web page displayed in the view. However, if you want to prevent downloads in your WebView, you can do so by modifying the WebViewClient associated with the view.

Here's how to disable downloads in Android WebView:

In your activity's Java file, add the following code to create a custom WebViewClient:
WebView webView = findViewById(R.id.webView); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.endsWith(".pdf") || url.endsWith(".doc") || url.endsWith(".docx")) { Toast.makeText(MainActivity.this, "Downloads are disabled", Toast.LENGTH_SHORT).show(); return true; } return false; } });

In the above code, we override the shouldOverrideUrlLoading method of the WebViewClient. This method is called when a URL is about to be loaded in the WebView. We check the URL's file extension and if it ends with ".pdf", ".doc", or ".docx", we show a toast message indicating that downloads are disabled and return true. By returning true, we prevent the URL from being loaded in the WebView.

To load a web page in the WebView, you can use the loadUrl method as usual:

webView.loadUrl("https://www.example.com");

With these steps, you can successfully disable downloads in your Android WebView. You can customize the code to disable downloads for other file types or add a different behavior when a download is attempted.

Previous Post Next Post