The question:
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.
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 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;
}
});
Method 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;
}
});
Method 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);
}
});
Method 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
Method 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("#");
}
}
Method 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
}
}
}
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