logo

A View that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It uses the WebKit rendering engine to display web pages.

In this demo, we get to view a web address in a totally different way by sidestepping the default browser and creating our own custom webviewer.

The key to creating the custom webviewer is the MyWebViewClient class that extends the WebViewClient class. With its key method shouldOverrideUrlLoading(view,url) that gives control to application when a new url is loaded in the provided view.

Sample Example

1) Create a new project/Activity called UIActivity.

2) We will have EditText in which URL to be entered, Button and WebView.

So Open the layout file. Insert following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
    <LinearLayout 
    	android:orientation="horizontal"
    	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content">
  
	    <EditText  
	    android:id="@+id/url" 
	    android:layout_height="wrap_content" 
	    android:layout_width="wrap_content"  
	    android:lines="1"
	    android:layout_weight="1.0"   
	    android:hint="http://"/>
	    
	    <Button
	    android:id="@+id/go_button"
	    android:layout_height="wrap_content" 
	    android:layout_width="wrap_content"     
	    android:text="go"
	    />
	    
    </LinearLayout>
    
	<WebView  
		android:id="@+id/webview_compontent"
	    android:layout_width="fill_parent" 
	    android:layout_height="fill_parent"    
	/>
</LinearLayout>

3) Now open the UIActivity.java file. At the top of the class, instantiate a WebView object:

                private WebView webView;
		private EditText urlField;

Then add the following at the end of the onCreate() method:

                        webView  = (WebView) findViewById(R.id.webview_compontent);
		        urlField = (EditText)findViewById(R.id.url);

((Button)findViewById(R.id.go_button)).setOnClickListener(new View.OnClickListener(){

					@Override
					public void onClick(View v) {
						
						webView.loadUrl(urlField.getText().toString());
						
					}
		        });

This captures the WebView we created in our layout.
Here we can load a page, but as soon as we click a link, the default Android web browser handles the Intent, instead we will be handling actions in our own WebView.

4) Because we’re accessing the internet, we need to add the appropriate permissions to the Android manifest file. So open the AndroidManifest.xml file and, add the following as a child of the element:

<uses-permission android:name="android.permission.INTERNET" />

So now we’ll override the WebViewClient to enable us to handle our own URL loading.

In the UIActivity, add this nested private class:

private class MyWebViewClient extends WebViewClient {
	        @Override
	        public boolean shouldOverrideUrlLoading(WebView view, String url) {
	            view.loadUrl(url);
	            return true;
	        }
	    }    

Now, in the onCreate() method, set an instance of the HelloWebViewClient as our WebViewClient:

webView.setWebViewClient(new MyWebViewClient());

This line should immediately follow the initialization of our WebView object.

What we’ve done is create a WebViewClient that will load any URL selected in our WebView in the same WebView.

You can see this in the shouldOverrideUrlLoading() method, above—it is passed the current WebView and the URL, so all we do is load the URL in the given view. Returning true says that we’ve handled the URL ourselves.

5) Run the application.

AUTHOR: Vikas Hiran
No Comments

Leave a Comment

Your email address will not be published.