Online Tutorials & Training Materials |
Register Login

WebView Example in Android

|| || 0

WebView Example in Android

Android WebView Example in Android Activity

Please follow the steps given below in order to use webview in your Android Activity:

Step 1) So first of all, create a button and change its text to ‘open URL’ okay and also, another text view to change its property layout:width to fill_parent so that it covers all the width.

Step 2) Now go to the text field and take a text edit from there. We are taking this plain text but you are another text view or edit text also. Now in this edit text also please change the property for the layout_width or layout:width to fill_parent okay so it fills the width.

Step 3) Now the most important thing we are going to use here is the webview, so just search for a webview in the palette and drag and drop in your activity and then you can resize your webview and you can change the property of this webview to fill_parent so it can cover all the remaining area of the activity.

Step 4) Now w in this edit text, enter some default text so that we don’t have to type a long URL for it for the first time. So let’s open, for example, with this, therefore add the URL https:// and then,

Step 5) Now we are ready to write the code for our activity. So we will go to the Java and in the file, we will  declare these variable first for the button, second for edit text and the last for webview.

Step 6) Now we are going to make a function for the button click event and inside this function, we will cast all the three variables which we have declared, one is for button and edit text and browser.

Step 7. a) Now to open any URL or any webpage, you need to provide the permission to open or the permission to use the internet therefore go to your manifest folder in your app folder and in here, you will be able to see a folder called androidmanifest.xml and in here, you can set your permission.

Step 7. b) Just above our application element or tag we will provide our permissions. So in there, what you need to add this tag called <user-permission and then give the name android : name = "android.permission.internet" />.

Step 8) Now we will add the button click event to our button variable ie. button_sbm.setonclicklistener and in there set a new OnClickListener. And write some code (shown below) for our OnClickListener.

Step 9 a) Now take the browser variable and set some properties to this browser.

  • the first property we are going to add is for loading images
  • second property we are going to set is for the java script
  • the third property we are going to set is for the scroll bar

Step 9 b) Now we can load some URL into our browser therefore just call load URL function on our browser, load URL and it takes a string as an argument and we have already extracted our string or URL from our edit text in this URL right, so we can pass it here.

Step 10) Now just call this openUrl() function in your onCreate method and let’s run our program.

So our program is running now and you can enter any URL is the edit text. Our default URL is so let’s click this open URL button and we will see it opens our webpage in the webview.


Related Articles