Home > Net >  Links are loading in external app (Browser) issue
Links are loading in external app (Browser) issue

Time:06-14

I'm showing this website's "https://www.egkhindi.com/" homepage on my app by using this code

MainActivity.java

package com.example.appthree;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        String Htmlurl = "https://www.egkhindi.com/";
        WebView view = (WebView) this.findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl(Htmlurl);


    }
}

This is coming fine but when I'm clicking any link on app , an external browser comes up to load that link. Is there any fix to load every external link in my app ?

CodePudding user response:

for this, you have to create webViewController Class to load view inside app webview

Make changes in your code as below

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String Htmlurl = "https://www.egkhindi.com/";
        WebView view = (WebView) this.findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        view.setWebViewClient(new webViewController()); // added webViewController here  
        view.loadUrl(Htmlurl);


    }

    // Webview Controller to handle view inside app webview
    private static class webViewController extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) { 
            view.loadUrl(url); // to loading view inside webview and return true
            return true;
        }
    }
    
}

I hope it helps.

CodePudding user response:

use this settings for your webView :

kotlin:

view.webViewClient = object : WebViewClient() {
    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
        view.loadUrl(url)
        return true
    }               
}

java:

view.setWebViewClient(new mWebViewClient());
private static class mWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        view.loadUrl(url); // to loading view inside webview and return true
        return true;
    }
}
  • Related