Android UI How to - Load a URL with WebView








The following code shows how to load a URL with WebView.

Example

Add Internet to uses-permission in manifest xml file.

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

Layout xml

<?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"
  >
  <WebView android:id="@+id/webkit"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
  />
</LinearLayout>

Java code

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
//from  w w  w.j ava2  s .c om
public class MainActivity extends Activity {
  WebView browser;
  
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    browser=(WebView)findViewById(R.id.webkit);
    
    browser.loadUrl("http://java2s.com");
  }
}
null