Download Manager in WebView Android

Download Manager In WebView – Android Studio Tutorial

The download manager is a service that can be used to handle downloads. Set the download URL and when the Download manager is invoked it will download the file in the background. It needs permission to write into storage and permission to access the Internet. There are plenty of features for the download manager. You could take a look at the documentation on Download Manager for Android Developers here. You can implement this on a new project or even on your existing one. I would be implementing Download Manager in WebView. Let’s start by creating a new Android Studio Project.

Download Source Code – Download Manager Android

Permissions needed

First of all, let’s add all the permissions that we need to Manifest File. Go to the app ->manifest -> AndroidManifest.xml file and all the following permissions.

Seeing these you might have understood for what these permissions are needed. They are for accessing the Internet, Writing into external storage and to access network state.

Now on the XML part, we have to Create a WebView and give it an ID. Go to app -> res -> layout -> activity_main.xml. Then call a WebView and give it an ID, I am giving it the ID web.

XML Part for WebView

That’s all that you have to do in the XML Part. Now we have added XML part of the WebView everything else is done in Java. So go to app -> java -> com.codeseasy.downloadmanager -> file. Now let’s add the Java part for WebView and also setup Download Manager for our WebView.

Java for Handling Download Manager in WebView

First, we need to ask permission for writing into external storage. This is needed for all the devices running Android Marshmallow and above. Add the codes outside the onCreate method and call the checkDownloadPermission() method inside the onCreate method.

We have to get the WebView through its ID and load a URL. First, create an object for WebView above the onCreate method. We also require the URL to be loaded on the WebView so add that variable also.

Now inside the onCreate() method, we have to initialize the webView with the ID we have given to our WebView in the XML part. “URL” is a string that contains the website URL which is defined in the previous step.

Now set JavaScript enabled for the WebView.

We have to make sure that the URLs load only inside our WebView. Add these codes to make the URLs to load inside the WebView itself.

That’s all on WebView, now we have to set up the download manager in the WebView. For that, we have to set a Download Listener for the WebView.

Inside the Download Listener, we have to create a new request for the Download manager. Add the codes inside Download Listener.

We can give a Title and Description for the download.

If you want a notification on the notification bar saying that “the download is successful” add this line of code.

You can also set the destination directory for the downloads. For that, we just need to specify the directory for the download manager.

Now just call the download service which is there in Android and start the download.

You can also make a toast message when the downloading starts.

If you need to show a toast after the download you can use a Broadcast Receiver for it. An error will occur at onComplete, it will be resolved in the next step.

You have to close the onDownloadStart and add the following.

That’s all you have set up the download manager for the WebView. I will provide all the codes in MainActivity together so that you could have a look.

Download Source Code – Download Manager Android

That’s all you have successfully setup download manager in WebView. Now you can download any file directly from your WebView itself.

Leave a Comment