View Full Version : Need help with code for WebView

08-24-2011, 11:15 PM
I am attempting to familiarize myself with eclipse/phonegap and the SDK and AVD to write my own App and I am stuck because of my lack of knowledge as it relates to Java coding. Following the tutorial on the Android Developers site I have so far an App.java file that looks like this:

package com.phonegap.HelloWebView;

import android.os.Bundle;
import com.phonegap.*;

public class App extends DroidGap {
/** Called when the activity is first created. */

public void onCreate(Bundle savedInstanceState) {

Now I realize that I am at risk of crossing the line in the rules that says I can ask for help "within reason" but I am desperate. Without taking a full course on Java I don't know how else to get this question answered so please take pity on me and someone give me an idea of how to appy the instructions from 3. on, to this App.java file.:android-sad: Thank you in advance for any help you might be able to offer.

I did steps 1 and 2 to make the .xml file.
Now get to my headache :android-cry:

3. Now open the HelloWebView.java file. At the top of the class, instantiate a WebView object:

WebView webview;
Then add the following at the end of the onCreate() method:

webview = (WebView) findViewById(R.id.webview);

This captures the WebView we created in our layout, then requests a WebSettings (http://developer.android.com/reference/android/webkit/WebSettings.html) object and enables JavaScript. Then we load a URL

4. In the HelloAndroid Activity, add this nested private class:

private class HelloWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}Now, in the onCreate() method, set an instance of the HelloWebViewClient as our WebViewClient:
webview.setWebViewClient(new HelloWebViewClient());

08-29-2011, 10:57 PM
Can you link to the tutorial you are using?

And describe the problem you are stuck on? :)

08-30-2011, 04:04 PM
I finally figured this out :android-rules:and origainally my problem was that I didn't even know enough about Jave to know how to insert the code w/o generating errors. I finally solved that problem but when it would load the app it would load the title and a black screen instead of the page i indicated. That problem was solved by using "fill_parent" instead of "wrap_content" in the <Linear layout> section of Main.xml....The final code to make this work went as follows.
The WebViewDemo.Java file should look like this:

package com.androidworkz.webview;import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; /** * Demonstrates how to embed a WebView in your activity. Also demonstrates how * to have javascript in the WebView call into the activity, and how the activity * can invoke javascript. * <p> * In this example, clicking on the android in the WebView will result in a call into * the activities code in {@link DemoJavaScriptInterface#clickOnAndroid()}. This code * will turn around and invoke javascript using the {@link WebView#loadUrl(String)} * method. * <p> * Obviously all of this could have been accomplished without calling into the activity * and then back into javascript, but this code is intended to show how to set up the * code paths for this sort of communication. * */ public class WebViewDemo extends Activity {private static final String LOG_TAG = "WebViewDemo";private WebView mWebView;private Handler mHandler = new Handler(); @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); mWebView = (WebView) findViewById(R.id.webview); WebSettings webSettings = mWebView.getSettings(); webSettings.setSavePassword(false); webSettings.setSaveFormData(false); webSettings.setJavaScriptEnabled(true); webSettings.setSupportZoom(false); mWebView.setWebChromeClient(new MyWebChromeClient()); mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo"); mWebView.loadUrl("http://hs.harrytennant.com/cooper_ap_home/29homeM.cfm"); }final class DemoJavaScriptInterface { DemoJavaScriptInterface() { } /** * This is not called on the UI thread. Post a runnable to invoke* loadUrl on the UI thread. */ public void clickOnAndroid() { mHandler.post(new Runnable() { public void run() { mWebView.loadUrl("javascript:wave()"); } }); } } /** * Provides a hook for calling "alert" from javascript. Useful for * debugging your javascript. */ final class MyWebChromeClient extends WebChromeClient {@Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) {Log.d(LOG_TAG, message); result.confirm(); return true; } } }

The Main.xml file should look like this:

<?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/webview"android:layout_width="fill_parent"android:layout_height="fill_parent"/></LinearLayout>
And the AndroidManifest.xml should look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.androidworkz.webview"android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".WebViewDemo"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-sdk android:minSdkVersion="7" /><uses-permission android:name="android.permission.INTERNET"></uses-permission></manifest>

08-31-2011, 12:53 AM
Cool. Glad you got it sorted :)