Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does WebView need a WebViewClient to work?

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.

like image 745
Arnab Chakraborty Avatar asked Jan 05 '12 09:01

Arnab Chakraborty


People also ask

How does a WebView work?

The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.

What is the use of WebViewClient in Android?

What is WebViewClient and what is it's usage? When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL.

How do I run WebView?

Modify src/MainActivity. java file to add WebView code. Run the application and choose a running android device and install the application on it and verify the results. Following is the content of the modified main activity file src/MainActivity.

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.


2 Answers

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.

like image 200
ChristopheCVB Avatar answered Oct 02 '22 18:10

ChristopheCVB


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);
like image 34
java dev Avatar answered Oct 02 '22 20:10

java dev