Link should be open in same web view in Android

I am new in Android and I am trying to open a Link in webview using this code

WebView myWebView = (WebView) findViewById(R.id.webinfo);
    myWebView.loadUrl("https://oslobokfestival.netteam.no/artical.php?articalid=93");
    myWebView.setBackgroundResource(R.drawable.lbg);
    myWebView.setBackgroundColor(Color.TRANSPARENT);
    myWebView.getSettings().setJavaScriptEnabled(true);

and in this HTML page contains some links and I want that when user click that link should be open in same webview,
at this point its opening in mobile browser, please give me appropriate solution..
Thanks.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You need to add WebViewClient to your WebView in order to open it in the WebView. Something like

myWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        view.loadUrl(request.getUrl().toString());
        return false;
    }
});

Solution 2

Try remove @Overrideand an put it after loadurl
This work for me…

myWebView.loadUrl("https://someurl.com");
myWebView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
        viewx.loadUrl(urlx);
        return false;
    }
});

Solution 3

For me its works by simply overriding

shouldOverrideUrlLoading methods and

return super.shouldOverrideUrlLoading(view, request)

that will handle all links in same WebView.

webview.setWebViewClient(new WebViewClient(){
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
         return super.shouldOverrideUrlLoading(view, request);
      }
});

Solution 4

Override the method shouldOverrideUrlLoading of WebViewClient like this:

myWebView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.toString());
            return true;
        }
    });

and add this tag <uses-permission android:name="android.permission.INTERNET" /> in your manifest file To get access to the internet

Solution 5

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl("#");
    }
}

Solution 6

Here is how to do it kotlin and also take care of the Versions below 21:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            myWebView.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(
                    view: WebView,
                    request: WebResourceRequest
                ): Boolean {
                    view.loadUrl(request.url.toString())
                    return false
                }
            }
        } else {
            myWebView.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                    view?.loadUrl(url.toString())
                    return false
                }
            }
        }

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply