Isrg KB
The Best Way to Gain Knowledge

Android WebView with Download, Upload, JavaScript Alert, HTML5 Video Support

In Android, app development is pretty easy and fast with WebView which let you load your custom HTML code or the website, but there are certain limitations to the WebView which include lacking features like the file download, file upload, JavaScript alert and HTML5 video support including YouTube.

In this tutorial, we would be exploring WebView ahead while adding file download, file upload, JavaScript alert and HTML5 video support using custom and modified Android Java codes. We would also be using an example Android project to test out these features.


  • Knowledge of Java (Android)
  • Understanding of Android Project structure
  • Knowledge of HTML and JavaScript

Adding required permissions to AndroidManifest.xml

You need to add Internet access users-permissions (android.permission.INTERNET) in your AndroidManifest.xml to grant permission to access the Internet.

Running app without this permission would crash your app again and again without a proper error indication.

Creating WebView Activity

Add an empty activity to your project from >Android>Empty Activity and add the following XML code to your newly created activity layout file:

Once you’ve added the above WebView layout to your main activity where you would be displaying the HTML pages, the next step is adding the necessary Java code to handle the various events.

Declare the following variables in the main public class:

Define the ProgressDialog after setContentView(…) as:

Define WebView as:

Adding download file capability/ support: 

All of the above code except the declaration part to be added within the scope of onCreate() method.

Overriding WebViewClient (should be written within the main class but outside the scope of onCreate() method):

You need to override the WebViewClient to open the links within the app instead of opening in Chrome or your default web browser. By overriding WebViewClient. You can also handle the events such as onPagestarted() which is useful when you need to display the Progress bar or find when the page has started loading, onPageFinishied() fires when the page finish loading the content using which you can hide the progress bar. onReceivedError() is very useful especially when you want to display custom “404 messages” or tell the user that there are Internet issues that need to be fixed as well as you can hide the Progress bar if there any error occurs in loading the page.

Adding upload file support by overriding WebChromeClient (should be written within the main class but outside the scope of onCreate() method):

By overriding WebChroeClient() you not only add the feature JavaScript alert support but also you can add the file upload support with the help of startFileChooserIntent() and onActivityResult().

Handing on back press button:

You may be looking for the functionality that can take the user to the previous page and if there is no previous page then close the current activity; well, you can achieve this by overriding the onBackPressed() method as:

Adding HTML5 audio and video play support, including YouTube, you need to enable hardware acceleration by adding the following code in your Manifest.xml file.

You can find the WebView Download and upload sample project by Isrg Rajan on the GitHub