selva
selva

Reputation: 1503

Android WebView click open within WebView not a default browser

I did one sample application using WebView, in that web view the URL comes from web services. It's working fine, but if I click any link within that WebView, its automatically go for default web browser. But I want to open within my application web view only. Here my code:

WvBikeSite = (WebView) findViewById(R.id.wv_bikeWebsite);
wvBikeSite.loadUrl(getBundle.getString("www"));

Upvotes: 34

Views: 84237

Answers (12)

Sri Krishna Chaitanya
Sri Krishna Chaitanya

Reputation: 73

Use this code:

// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());       

Upvotes: 3

Mark Lyons
Mark Lyons

Reputation: 1422

You need to set up a WebViewClient in order to override that behavior (opening links using the web browser). You obviously have your WebView declared, but then set up a WebViewClient like so:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());

Then you need to define your WebViewClient():

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // Designate Urls that you want to load in WebView still.
            return false;
        }

        // Otherwise, give the default behavior (open in browser)
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

Then start your WebViewClient:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());

http://developer.android.com/guide/webapps/webview.html

Upvotes: 12

Md Imran Choudhury
Md Imran Choudhury

Reputation: 9997

Most of the answer is right but please notice that: SupportMultipleWindows must be set as false.

mWebView.getSettings().setSupportMultipleWindows(false);

and now set webViewClint and get loading URL.

mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){

    Log.d(TAG,"URL: "+url);
    view.loadUrl(url);

    /**
     * if you wanna open outside of app
     if (url != null && url.startsWith(URL)) {
     view.loadUrl(url);
     return false;
     }
     // Otherwise, give the default behavior (open in browser)
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
     startActivity(intent);**/
    return true;

}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
    super.onPageStarted(view, url, favicon);
}

@Override
public void onLoadResource(WebView view, String url) {
    super.onLoadResource(view, url);
}

@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);

    }
});

Upvotes: 0

Ashish Verma
Ashish Verma

Reputation: 11

Try this one, that method is deprecated.

public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }

Upvotes: 1

pvrforpranavvr
pvrforpranavvr

Reputation: 2938

webview.setWebViewClient(new WebViewClient());
webview.loadUrl(url);

Do set webview client.

Upvotes: 1

Amrish Kakadiya
Amrish Kakadiya

Reputation: 1023

This Will open any particular link only in your app ...

WebView web;

web = (WebView) findViewById(R.id.web);
web.setWebViewClient(new WebViewClient(){

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && url.startsWith("http://www.xplorerstrick.net")) {
            view.loadUrl(url);
            progDailog.show();
            return true;
        }
        else {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
}

Upvotes: 0

Sharan
Sharan

Reputation: 1

Here is my code how to resolve the above problem (when cliking on link it asking for default browser to opn the link)

import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;
import android.view.Menu;import android.webkit.WebChromeClient;
import android.webkit.WebView;public class MainActivity extends Activity{
`@SuppressLint("SetJavaScriptEnabled")@Override protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String url="http://google.com";WebView Webview=(WebView) this.findViewById(R.id.webView1);     Webview.getSettings().setJavaScriptEnabled(true);Webview.loadUrl(url);}@Override    public boolean onCreateOptionsMenu(Menu menu){//Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu);return true;}`}

Upvotes: 0

Ali
Ali

Reputation: 835

I did like this and its working perfect..

mWebView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url != null && url.startsWith("http://")) {
                mWebView.loadUrl(url);
                return true;
            } else {
                return false;
            }
        }
    });

Upvotes: 1

Syed_Adeel
Syed_Adeel

Reputation: 430

I face same problem and i just fixed it by adding single line.

webview.setWebViewClient(new WebViewClient());

and this solved my problem.

Upvotes: 6

Philip Sheard
Philip Sheard

Reputation: 5825

You need to call wvBikeSite.setWebViewClient, e.g:

    MyWebViewClient wvc = new MyWebViewClient();
    wvBikeSite.setWebViewClient(wvc);

Where MyWebViewClient overrides shouldOverrideUrlLoading, viz:

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

Upvotes: 2

Terence Lui
Terence Lui

Reputation: 2808

You have to set up a webViewClient for your webView.

Sample:

this.mWebView.setWebViewClient(new WebViewClient(){

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

Upvotes: 74

saba
saba

Reputation: 430

You can always open url within webview by using this:

      @Override
  public void onCreate(Bundle savedInstanceState) {
     webview.setWebViewClient(new MyWebViewClient());
 webview.getSettings().setJavaScriptEnabled(true);
 webview.getSettings().setPluginsEnabled(true);
     }


     private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {


            view.loadUrl(url);
            return true;
        }

        @Override
        public void onLoadResource(WebView  view, String  url){

        }
    }    

Upvotes: 1

Related Questions