Opening webview not in new browser

Ya. You must implement WebViewClient class and Override shouldOverrideURLLoading() method in this class.

Why ? Because webview just open your "exactly link", if that link redirect other links, android will open default browser for this action.

In your example, as you know, when you connecting to google.com google will redirects to google at your country. Example, if you are in China, google will go to google.com.cn, if in Vietnam, will be google.com.vn.

Here is my simple example: (you can imagine this is an new browser, :laugh)

First is layout xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

        <EditText 
            android:id="@+id/url"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:hint="Input URL"/>

        <Button 
            android:id="@+id/run"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="0"
            android:text="GO"/>

    </LinearLayout>

    <WebView 
        android:id="@+id/webview"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"/>

</LinearLayout>

Here is code of main activity:

package com.basic;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class WebViewExample extends Activity{ 

    WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

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

        Button button = (Button) findViewById (R.id.run);
        button.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {
                gotoPage();             
            }
        });

    }

    private void gotoPage(){

        EditText text = (EditText) findViewById(R.id.url);
        String url = text.getText().toString();

        WebSettings webSettings = webView.getSettings();
        webSettings.setBuiltInZoomControls(true);

        webView.setWebViewClient(new Callback());  //HERE IS THE MAIN CHANGE
        webView.loadUrl(url);

    }

    private class Callback extends WebViewClient{  //HERE IS THE MAIN CHANGE. 

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return (false);
        }

    }

}

Hope this help you :)


Adding the following code before loadUrl() will solve this problem,

wv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
          view.loadUrl(url);
          return true;
           }}); 

The shouldOverrideUrlLoading() from WebViewClient does this job. Here goes the Android doc for shouldOverrideUrlLoading,

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...

http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29


My XML implementation:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent">

<WebView
    android:background="@android:color/transparent"
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></WebView>
</RelativeLayout>

My Java implementation:

WebView webView;
public final String GlobalUrl = "http://slashdot.org/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_application_activity);

    webView = (WebView) findViewById(R.id.webView);
    loadWebViewLoad(webView);
}

private void loadWebViewLoad(WebView webview) {
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webview.getSettings().setSupportMultipleWindows(true);
    webview.setWebViewClient(new WebViewClient());
    webview.setWebChromeClient(new WebChromeClient());
    webview.loadUrl(GlobalUrl);
}

And final result is:

Example image