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 🙂

Leave a Comment