0

I am making an app for WebView with a header progress bar. It is working nicely but after loading the web page, if I click any link on the webpage, the app closes and opens the default browser. But instead, I want to stay in the WebView page after the click and open the new tab on the same page.

How can I do this?

    public class MainActivity extends Activity {

        private WebView webView;
        private EditText urlEditText;
        private ProgressBar progress;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

           // urlEditText = (EditText) findViewById(R.id.urlField);
            webView = (WebView) findViewById(R.id.webView);
            //webView  = new WebView(this);
            progress = (ProgressBar) findViewById(R.id.progressBar);
            progress.setMax(100);

            webView.setWebChromeClient(new MyWebViewClient());


            //Button openUrl = (Button) findViewById(R.id.goButton);


                    String url = "http://amrtube.com/result/index.php";
                    if (validateUrl(url)) {
                        webView.getSettings().setJavaScriptEnabled(true);
                        webView.getSettings().setJavaScriptEnabled(true); // enable javascript
                        this.webView.getSettings().setAllowFileAccess(true);
                        this.webView.getSettings().setBuiltInZoomControls(true);
                        //this.mWebview.getSettings().setSupportZoom(true);
                        final Activity activity = this;
                        this.webView.setScrollBarStyle(33554432);
                        this.webView.setScrollbarFadingEnabled(false);
                        webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);

                        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

                        webView.loadUrl(url);

                        MainActivity.this.progress.setProgress(0);
                    }




                }

                private boolean validateUrl(String url) {
                    return true;
                }




        private class MyWebViewClient extends WebChromeClient {





            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.toString());
                return true;
            }


            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                MainActivity.this.setValue(newProgress);
                super.onProgressChanged(view, newProgress);
            }
        }
        public void setValue(int progress) {
            this.progress.setProgress(progress);
        }
    }
Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
babu
  • 1
  • 5
  • try changing WebChromeClient to WebViewClient – Nabil Jun 25 '18 at 05:37
  • 2
    Possible duplicate of [Link should be open in same web view in Android](https://stackoverflow.com/questions/7308904/link-should-be-open-in-same-web-view-in-android) – Kunu Jun 25 '18 at 05:50
  • Change the title of your question, it is misleading. ProgressBar has nothing to do with your question. – Kunu Jun 25 '18 at 05:51
  • Itry it but header progress bar not working on WebViewClient – babu Jun 25 '18 at 05:57

1 Answers1

0

Replace your line with this

this.mWebView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
Jaimil Patel
  • 1,301
  • 6
  • 13