开发者

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);
    myWebVi开发者_JAVA百科ew.loadUrl("http://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.


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;
    }
});


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

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


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);
      }
});


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


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("#");
    }
}


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
                }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜