|
|
Press the "Update to Google" button to have the WebView switch from website http://brainyideas.com to http://google.com . The WebView is setup with JavaScript and zooming capability.
package com.brainyideas.dev.poc.bpwvu;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class WebViewUpdate extends Activity {
Button captureButton;
WebView webview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
void init() {
captureButton = (Button) findViewById(R.id.updateWV);
captureButton.setOnClickListener(listener);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new HelloWebViewClient());
// allow zoom and enable multitouch if supported by ROM
webview.getSettings().setSupportZoom(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.loadUrl("http://brainyideas.com");
}
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
webview.loadUrl("http://www.google.com");
}
};
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}<?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"> <Button android:id="@+id/updateWV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Update to Google"/> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </ScrollView> </LinearLayout>