Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Accessing a Web Page Using WebView

Author: Rachee Singh
Published? true -- FormatLanguage: W

Problem:

Opening a website within your application.

Solution:

Embed a WebView component in the layout and use it to display the web page.

Discussion:

In the application, since we wish to access a web page, we need to add the Internet permission into the manifest file:

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

The component which is used to display a web page within an application is called a WebView. We add a WebView to the XML layout:

<WebView
android:id="@+id/webview"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>

In the Java code for the activity that displays the web page, we obtain a handle onto the WebView using findViewById() method. On the WebView we use the loadUrl method to provide it the URL of the web site we wish to open in the application.

WebView webview = (WebView)findViewById(R.id.webview);
webview.loadUrl("http://google.com");

Download:

The source code for this project can be downloaded from https://docs.google.com/leaf?id=0B_rESQKgad5LN2JhMDFjZTUtY2IwZS00NzkyLWFlNjItMzhiZWRlYTQxMWNm&hl=en_US.
No records found.