11

I was going through android tutorials and tried out the WebView example. This is what I ended up with:

WebAppActivity

public class WebAppActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView wv = (WebView) findViewById(R.id.webView1);
        wv.loadUrl("http://www.google.com");

    }
}

main.xml

<?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/webView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </WebView>

</LinearLayout>

But instead of loading the page in the application itself, as soon as the application starts the default android browser opens and the page loads in the browser instead of the application. When I press back I return to the application activity which displays a blank screen.

Does anyone know why this is happening?

Edit:

manifest

<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"/>

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

This was just to show that I have added the INTERNET permission

Edit :

As soon as I add a WebViewClient,

wv.setWebViewClient(new WebViewClient() {});

the page loads in the application. Is this expected behaviour? Does an Android WebView require a WebViewClient? (couldn't find any documentation on it)

Edit :

I noticed that this problem occurs when I install the apk in an emulator which has the Google APIs. On a normal emulator (without the Google APIs) it behaves as expected.

Arnab Chakraborty
  • 7,442
  • 9
  • 46
  • 69
  • 1
    your question is already answered on: http://stackoverflow.com/questions/2378800/android-webview-click-opens-default-browser – ariefbayu Jan 05 '12 at 09:43
  • @silent Please read carefully. That is not my question. I am having trouble loading the first page itself, which loads in the browser instead of the application. See Flo's comment – Arnab Chakraborty Jan 05 '12 at 09:48
  • 1
    No I don't think he's talking about links in the web page itself that got clicked. This sounds like the web page is never loaded in the WebView at all. – Flo Jan 05 '12 at 09:49
  • Here is a detailed example: **[Android - WebView client example](http://goo.gl/zcWUq)**, agree with you @silent. – Paresh Mayani Jan 05 '12 at 09:58
  • @PareshMayani Even the first page isn't loaded. I am talking about link clicks. The very first page I am trying to load using `webView.loadUrl("http://google.com")` isn't being loaded. It directly invokes the browser. Is that expected behaviour? – Arnab Chakraborty Jan 05 '12 at 10:03
  • @PareshMayani: Deleted my answer as it's of no use anymore.. :) +1@Aki for question edit.. – Ghost Jan 05 '12 at 11:31
  • Please refer my answer below. It may help you understand why the first page doesn't load in you application. – Sagar Waghmare May 01 '13 at 08:21

5 Answers5

4

Yes, you have to set a WebViewClient that returns true on the overridden method 'shouldOverrideUrlLoading' so that your webview loads the URL in your app.

Let me know if you want an example.


Edit

@Aki WebViewClient.shouldOverrideUrlLoading Documentation

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url.

Marie
  • 194
  • 6
ChristopheCVB
  • 7,269
  • 1
  • 29
  • 54
  • @Christopher Why is it that we need a webViewClient only in case of devices with google apis. Moreover, can you point me to some official Android documentation which states this fact? – Arnab Chakraborty Jan 12 '12 at 04:30
  • Thanks Christopher. But it does not say anywhere that `shouldOverrideUrlLoading` is called when we invoke the `loadUrl` method. – Arnab Chakraborty Jan 12 '12 at 12:08
1

For loading a webpage from url into a webview, there is no need to setting webview client. Without webview client you can load a webpage into your webview. But WebViewClient brings many advantages for handling the webview. Sample usage for loading webpage from url,


webView.loadUrl(yoururl);
java dev
  • 1,044
  • 1
  • 11
  • 17
0

The only reason the url is opened in default android browser is because of "wv.loadUrl("http://www.google.com");"

When you load http://www.google.com, the google website actually redirects the page to http://www.google.co.in (assuming you are launching the app from India).

If you run "wv.loadUrl("http://www.google.co.in");", google will not redirect the page and the first page will be opened in your application and further clicks will be opened in the system browser.

To handle this further clicks you need the WebViewClient.

Sagar Waghmare
  • 4,702
  • 1
  • 19
  • 20
0
private WebView webVenue;
private WebSettings websettings;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.schedule_time);

        webVenue = (WebView)findViewById(R.id.webview_schedule_time);
        txtEmptyMsg = (TextView)findViewById(R.id.txtEmptyMsg);

        mContext = this;        
        webVenue.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webVenue.getSettings().setJavaScriptEnabled(true);
        websettings=webVenue.getSettings();
        webVenue.setScrollBarStyle(ScrollView.SCROLLBARS_OUTSIDE_OVERLAY);
        webVenue.loadUrl(URL);
}
}

All The Best...

Richa
  • 3,165
  • 1
  • 22
  • 26
0

No not quite but it allows you to do a lot of stuff.

Note that making a call to shouldOverrideUrlLoading in the WebViewClient doesn’t seem to work either, so you should do your processing in onPageFinished.

Here is a blog post that'll guide you through.

AbdulFattah Popoola
  • 949
  • 2
  • 13
  • 22