Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent WebView from Opening the Browser

I'm already active JavaScript for a given WebView, and opens new link inside the WebView, not in the Browser. This Is Main Activity

    package com.Afrogfx.pronouns;

    import android.os.Bundle;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.view.Menu;
    import android.webkit.WebView;

    @SuppressLint("SetJavaScriptEnabled")
    public class MainActivityPronouns extends Activity {

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

    WebView wvHtml = (WebView) findViewById(R.id.webview);
    wvHtml.getSettings().setBuiltInZoomControls(true);
    wvHtml.getSettings().setJavaScriptEnabled(true);
    wvHtml.loadUrl("http://afrogfx.com/appcatcategories.php?catid=13&parentid=11");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main_activity_pronouns, menu);
    return true;
    }
    }

how can i handel my code to open all link in site inside the WebView (App) , not in the Browser & ( don't show to user Open in browser).

like image 659
Afro Gfx Avatar asked Nov 10 '12 11:11

Afro Gfx


People also ask

How do I block WebView?

Tap Android System WebView and then tap Disable.

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.

What is the difference between a WebView and a browser?

Android System WebView lets applications display browser windows in an app instead of transporting the user to another browser. Android developers use WebView when they want to display webpages in a Google app or other application.


2 Answers

For that just create the subclass that is extending webclient and use the method of that class onPageFinished(WebView c,String url) and
public boolean shouldOverrideUrlLoading(final WebView view, final String url)

here is the code-

 myWebView.setWebViewClient(new WebViewClient()       
        {
             @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) 
            {
                //view.loadUrl(url);
                System.out.println("hello");
                return false;
            }
        });
        myWebView.loadUrl(url);
like image 92
Ravi Avatar answered Oct 10 '22 08:10

Ravi


In case you use Kotlin:

webView.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(
                    view: WebView?,
                    request: WebResourceRequest?
                ): Boolean {
                    return false
                }
            }
like image 34
Dievskiy Avatar answered Oct 10 '22 10:10

Dievskiy