• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Webview Help

I was reading about WebViews in android application development and I am confused. I read on the guide (Building Web Apps in WebView) that I am supposed to place:
Code:
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
into my activity XML file (which I have done).

Then it says to:
To load a web page in the WebView, use loadUrl(). For example:
Code:
WebView myWebView =(WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");

But I am confused on how to do that. I assumed that meant just copy that in my activity_main.xml (my activity file), so I did, but it only created errors. So how do I do use loadurl()? Please describe it like you were describeing it to someone who does not know anything about development (because that is pretty much what you are doing, I am VERY new to development).

Thank you for your time.
 
Okay, you are not too far off but you are misunderstanding a few things. The activity_main.xml is NOT your activity file. This is your activities layout file.

The activity file would be similar to this:

Java:
public class WebViewActivity extends Activity {

     private WebView wv;

     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          // The id is from your line 3 in the layout xml file.
          wv = (WebView) findViewById(R.id.webview);

          // Extra settings you may want to test.
          //wv.getSettings().setJavaScriptEnabled(true);
          //wv.getSettings().setLoadWithOverviewMode(true);
          //wv.getSettings().setUseWideViewPort(true);
          //wv.getSettings().setBuiltInZoomControls(true);

          wv.loadUrl("http://androidforums.com");
     }

}

Hope that helps and good luck!
 
Thanks, but if activity_main.xml is not my activity file that which file is it?

Like I said in my last post, this is your activities layout file.

Directory structure example
src/com/example/structure/WebViewActivity.java (main activity to initialize and setup all the WebView code)
res/layout/activity_main.xml (layout file the main activity uses for its contentView which contains the webView widget)
 
Okay I think I got what you mean. I added an activity by clicking "New" then "Android Activity" and then edited the new java file it created to get the file in the screenshot. The only problem is it has created errors (I am probably doing something wrong, as I stated before I am very new to development). Please tell me what I am doing wrong.

Capture.PNG
 

Attachments

  • Capture.PNG
    Capture.PNG
    98.2 KB · Views: 65
It seems like the Android SDK is not detected or you need to import the needed references. If you hover over Activity or the info icon (lightbulb with a x), does it have import class as an option?

In the top of every .java file you should see: package com.example.testproject
..along with all the imported classes needed from the SDK. It doesn't look like this is the case from the screenshot.

I would honestly try to download Android Studio rather than Eclipse and they have some excellent new template projects that point you in the right direction. Could explain a lot and connect the dots for you.
 
Back
Top Bottom