Android WebView

Android WebView

Android’s WebView allows you to open an own windows for viewing URL or custom html markup page.

WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView. WebView makes turns your application to a web application.

In order to add WebView to your application, you have to add <WebView> element to your xml layout file. Its syntax is as follows −

In order to use it, you have to get a reference of this view in Java file. To get a reference, create an object of the class WebView. Its syntax is −

In order to load a web url into the WebView, you need to call a method loadUrl(String url)of the WebView class, specifying the required url. Its syntax is –

Apart from just loading url, you can have more control over your WebView by using the methods defined in WebView class. They are listed as follows −

canGoBack()This method specifies the WebView has a back history item.
canGoForward()This method specifies the WebView has a forward history item.
clearHistory()This method will clear the WebView forward and backward history.
destroy()This method destroy the internal state of WebView.
findAllAsync(String find)This method find all instances of string and highlight them.
getProgress()This method gets the progress of the current page.
getTitle()This method return the title of the current page.
getUrl()This method return the url of the current page.

If you click on any link inside the webpage of the WebView , that page will not be loaded inside your WebView. In order to do that you need to extend your class fromWebViewClient and override its method. Its syntax is −


In this tutorial, we are going to learn how to use WebView in our app to open URLs inside our App.

Video Demo

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is

2. To open url in WebView we need Internet Connection so for that we have to give “INTERNET” permission in Manifest file also we are checking internet connection before loading any url so for that we need “ACCESS_NETWORK_STATE” permission in Manifest file.

3. Create a xml layout naming activity_main.xml this layout contains edittext for input of url and button to load url in WebView below the button.

4. Finally come to your and add the following code. In this code we validate the entered url on button click before loading over webview and also checking internet connection.

If you are unaware of how to check internet connection then you can read my tutorial here.

5. Now, you are all done, run your app and you will get the output as shown in video. (Make sure to put http:// or https:// before loading url).

Thanks. :)

Download Source Code


About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)