Android UI How to - Get web settings from WebView








The WebView enables you to embed a web browser in your activity.

The following code shows how to get web settings from WebView.

Example

In the AndroidManifest.xml file, add the following permission:

        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.java2s.WebView"
            android:versionCode="1"
            android:versionName="1.0" >

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

            <application
                android:icon="@drawable/ic_launcher"
                android:label="@string/app_name" >
                <activity
                  android:label="@string/app_name"
                  android:name=".WebViewActivity" >
                  <intent-filter >
                      <action android:name="android.intent.action.MAIN" />

                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
          </application>

      </manifest>

Main layout xml file

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
            android:orientation="vertical" >

        <WebView android:id="@+id/webview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

      </LinearLayout>

In the MainActivity.java file, add the following statements:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
//from  w w w.ja  v  a  2s .  com
public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView wv = (WebView) findViewById(R.id.webview1);

        WebSettings webSettings = wv.getSettings();
        webSettings.setBuiltInZoomControls(true);
        wv.loadUrl("http://java2s.com/");
    }
}
null