Opening webview not in new browser

I am implementing a webview. I want two buttons on the top of the web page.

I have one vertical linear layout, inside of which is one horizontal layout with two buttons, and one webview outside of the horizontal layout.

I am simply loading the Google URL in Java code.

Every time I run the application, it opens a new browser on top of the application and the buttons get hidden. It's not showing the buttons above the webview. Please help and tell me how can I load a URL in the webview without opening another browser, or how I can prevent it by opening a native browser, so that the page is loaded in the webview itself and not a new browser.

Thanks all


Solution 1:

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 :)

Solution 2:

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