Need help with code for WebView

This is a discussion on Need help with code for WebView within the Beginning Developers forums, part of the Android Development category; 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 ...

Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By alostpacket

Thread: Need help with code for WebView

  1. #1
    Android Lurker Gort's Avatar
    Join Date
    Aug 2011
    Location
    Dallas, Texas, United States
    Posts
    4

    Question Need help with code for WebView

    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:
    Code:
    package com.phonegap.HelloWebView;
    
    
    import android.os.Bundle;
    import com.phonegap.*;
    
    
    
    
    public class App extends DroidGap {
        /** Called when the activity is first created. */
        @Override
    
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.loadUrl("file:///android_asset/www/index.html");
        }
    }
    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. Thank you in advance for any help you might be able to offer.

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

    3. Now open the HelloWebView.java file. At the top of the class, instantiate a WebView object:
    Code:
    WebView webview;
    Then add the following at the end of the onCreate() method:
    Code:
    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("http://www.google.com");
    This captures the WebView we created in our layout, then requests a WebSettings object and enables JavaScript. Then we load a URL

    4. In the HelloAndroid Activity, add this nested private class:
    Code:
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    Now, in the onCreate() method, set an instance of the HelloWebViewClient as our WebViewClient:
    Code:
    webview.setWebViewClient(new HelloWebViewClient());


  2. Android.net is the premier Android Forum. Registered users do not see these ads. .

  3. #2
    App Developer alostpacket's Avatar
    Join Date
    Jan 2011
    Location
    NY
    Posts
    240
    Thanked
    32 times
    Twitter
    alostpacket
    Can you link to the tutorial you are using?

    And describe the problem you are stuck on?
    Gort likes this.

  4. #3
    Android Lurker Gort's Avatar
    Join Date
    Aug 2011
    Location
    Dallas, Texas, United States
    Posts
    4
    I finally figured this out 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:
    Code:
    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:
    Code:
    <?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:
    Code:
    <?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>

  5. #4
    App Developer alostpacket's Avatar
    Join Date
    Jan 2011
    Location
    NY
    Posts
    240
    Thanked
    32 times
    Twitter
    alostpacket
    Cool. Glad you got it sorted

Remove Ads

http://www.scramblerducati.org/

Sponsored Links

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Similar Threads

  1. Browser Code
    By balamurugandk in forum Beginning Developers
    Replies: 2
    Last Post: 08-18-2011, 06:56 PM
  2. Browser Code
    By balamurugandk in forum Android Forum
    Replies: 0
    Last Post: 08-12-2011, 11:58 AM
  3. Replies: 2
    Last Post: 04-26-2011, 10:19 PM
  4. forgot lock code
    By carolyn in forum Android Forum
    Replies: 1
    Last Post: 03-01-2011, 03:36 PM

Search tags for this page

android asset razor problem
,
android nested webview example
,
android tab with webview sample code
,

android webview shouldoverrideurlloading example

,
android webview shouldoverrideurlloading not called
,
how to find webview in eclipse
,
kindle fire webview
,

phonegap android setwebviewclient

,

phonegap android webview

,

phonegap shouldoverrideurlloading

,

phonegap webview android

,

phonegap webview example

,

phonegap webview loadurl

,
phonegap webview sample
,

xml code for web view

Click on a term to search our sites for related topics.
Android Forum