PowenKo, Android Tutorial 054-2, internet, WebView in view


package com.powenko.Tutorial_UI_WebView_url;



import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Tutorial_UI_WebView_urlActivity extends Activity {
private WebView webview;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
   
        webview = (WebView) findViewById(R.id.webView1);  
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("http://www.powenko.com/en");

        webview.setWebViewClient(new HelloWebViewClient());
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}
<?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"
    >
    <android.webkit.WebView android:id="@+id/webView1" android:layout_width="fill_parent" android:layout_height="fill_parent"></android.webkit.WebView>

</LinearLayout>




<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.powenko.Tutorial_UI_WebView_url"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="3" />

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

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

sample code:
Tutorial_UI_WebView_url

reference:
http://developer.android.com/guide/tutorials/views/hello-webview.html


Leave a Reply