-1

I am trying to build a Webview app using android studio and I am currently having a problem. The index page shows perfectly on the app but whenever i click a hyperlink it redirects me to the browser and then to the website how can I get it to redirect me to another page whilst in the app built from webview. Here is a snippet of the main activity java

public class MainActivity extends AppCompatActivity {

private WebView mywebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mywebView = (WebView) findViewById(R.id.webview);
    WebSettings websettings=mywebView.getSettings();

    mywebView.loadUrl("https://www.cavaapperal.co.za/");
    websettings.setJavaScriptEnabled(true);
}
public class myWebClient extends WebViewClient{
    @Override
    public void onPageStarted (WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }
    @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
                return true;
    }

}

@Override
public void onBackPressed (){
    if (mywebView.canGoBack()) {
        mywebView.goBack();
    }    else{
            super.onBackPressed();
        }
    }

}

1 Answers1

0
webView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
            view.getContext().startActivity(
                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        } else {
            return false;
        }
    }
});

WebView link click open default browser

Kindly see the following link

M.zirie
  • 86
  • 2