-1

Every link the website has it opens it on the app itself, how can I make it so that it opens on a browser or the appropriate app? I have no idea how to do it and from what I have searched online I have only seen how to open links on your app and not on a browser. Thanks in advance. This is the MainActivity.java:

package com.example.website;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl("website.com");
        webView.setWebViewClient(new WebViewClient());
    }

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

    }
}
Dennis Kozevnikoff
  • 1,654
  • 3
  • 14
  • 24
k3v1
  • 3
  • 4

1 Answers1

0

Here it is, when clicking in links the new URL request is made. before making request it enters into shouldOverrideUrlLoading

webview.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url){
    String url2="https://whatoplay.com/";
 // all links  with in ur site will be open inside the webview 
 //links that start ur domain example(http://www.example.com/)
if (url != null && url.startsWith(url2)){
  return false;
} 
 // all links that points outside the site will be open in a normal android browser
else
{
  view.getContext().startActivity(
  new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
  return true;
}

} });

And My Refernce : webview-link-click-open-default-browser

Praveen
  • 139
  • 1
  • 7