Prevent WebView from Opening the Browser

24,210

Solution 1

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

Solution 2

In case you use Kotlin:

webView.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(
                    view: WebView?,
                    request: WebResourceRequest?
                ): Boolean {
                    return false
                }
            }
Share:
24,210
Admin
Author by

Admin

Updated on June 03, 2020

Comments

  • Admin
    Admin almost 4 years

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

  • Stan
    Stan over 8 years
    Welcome to "Only the original thread that created a view hierarchy can touch its views.” exception )
  • henon
    henon over 4 years
    The docs say you should return false to continue loading that url. returning true will abort loading
  • George Khouri
    George Khouri over 4 years
    I agree with @henon, it should be false