I have a PHP service that returns me HTML that I display in a WebView using
activity_news_complete_webview.loadDataWithBaseURL("", html, "text/html", "UTF-8", null);
Also implemented the WebViewClient as follows
WebViewClient webViewClient = new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
};
In doing so when i tap on any , the WebViewClient shows that the url attribute is about:blank but the tag contain proper urla in html variable which i load in the WebView.
Any one who can suggest me a proper solution for this would be a great help, I have read many threads on this issue but haven't found any proper solution because anything i try gives the same issue.
You can use this line replace to .loadDataWithBaseURL();
activity_news_complete_webview.loadData(html, "text/html; charset=utf-8", "UTF-8");
Its working perfectly for me. try this one.
Related
I can't open mht files in WebView.
It worked fine until Chrome v76, but v77 had a read error
If chrome is returned to v76, the read was successful.
file: /// also didn't work.
Loading with loadDataWithBaseURL also didn't work
webview.loadUrl("file://{$context.cacheDir}/file.mht")
cid: Frame- "Random string" # mhtml.blink
Net :: ERR_UNKNOWN_URL_SHCEME
Sorry for my late answer because i face same problem and i was searching solution yesterday and i resolve it today like below.
In your java class you have to give below code
webView.setWebViewClient(new WebViewClient() {
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
if (one_time==1)//one_time is only a variable because i want
// to run view.loadUrl(url); only one time.
view.loadUrl(url);
one_time++;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, final String url) {
}
});
I think you are giving
view.loadUrl(url);
in below method
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// view.loadUrl(url); like this
// you will need this when you will load the url but in case
//of mht page it will not work
return true;
}
do not do this if you are loading mht page because
shouldOverrideUrlLoading(WebView view, String url){...}
loads the url and it also run when first time you open webview activity.
so,
onLoadResource(WebView view, String url){...}
method load the Resource.
Trying to load this website with WebView, but it just show a blank page.
I can open other website with no problem, but not the above website. Possible the web developer block it? The website is not owned by me.
guideView = (WebView) findViewById(R.id.guideView);
guideView.setWebViewClient(new myWebClient());
guideView.getSettings().setJavaScriptEnabled(true);
guideView.loadUrl("https://sapsnkra.moe.gov.my/ibubapa2/index.php");
}
public class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
I tried to setWebChromeClient, but it do not work as well.
This link is apparently is blocked to access. I tried to access and dont work.
Make sure this .php is working to network access. As it is a .gov url there may be blocks.
This is the code part where I'm having the issues...
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.indexOf("tel:") > -1) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
return true;
} else {
return true;
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:replace('window.location.href', 'window.location')");
view.loadUrl("javascript:replace('document.location.href', 'document.location')");
}
});
I'm just adding on shouldOverrideUrlLoading the functionality to the webview to make calls when the link is a phone number, and adding some javascript code on onPageFinished to fix some issues that the webview has with window.location.href and document.location.href...
Thanks in advance for all the comments and replies, fixes and alternatives are welcome.
You can make new class which extends which extends WebViewClient.
Try this code snippet
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
}
Then used in your onCreate method.
webView.setWebViewClient(new MyWebViewClient());
In this both function are calling.CheckOut this.
Dhanyavaad!
My problem was in my shouldOverrideUrlLoading. I was returning true when the link is NOT a phone number, now if the url is not a phone number, returns super.shouldOverrideUrlLoading(view, url); and works perfectly.
Thanks Shashank your code open my eyes.
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.indexOf("tel:") > -1) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
return true;
} else {
return super.shouldOverrideUrlLoading(view, url);
}
}
});
Using webview.loadUrl(url) method I open an url. If I click any Button on the view it directs to another page. Now I want to get the url of the directed page. how can I get it?
I also want the content that is displayed on the webview. How to get the content of the WebView ?
String webUrl = webView.getUrl();
please see my answer
may it will help you...!!!!
WebView webview = new WebView(context);
webview.setWebViewClient(new WebViewClient()
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d("WebView", "your current url when webpage loading.." + url);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d("WebView", "your current url when webpage loading.. finish" + url);
super.onPageFinished(view, url);
}
#Override
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("when you click on any interlink on webview that time you got url :-" + url);
return super.shouldOverrideUrlLoading(view, url);
}
});
I am assuming that you have set your WebViewClient.If not then you can do like below,
webView.setWebViewClient(new MyWebViewClient());
String currentUrl;
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
currentUrl=url;
return true;
}
}
Here when you click on any link on the WebView then it will call shouldOverrideUrlLoading() and you can get the current url there in currentUrl.
this is the most simple way of handling this for API > 20: There are obviously other methods with parsers like HTMLCleaner to have more control. However, this is good and simple for getting URL. Each time URL changes in Webview, URL in the request object changes as well.
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String temp = request.getUrl().toString();
if(temp.isEmpty()){
Log.i(ActivityName, "No url returned!");
}else{
Log.i(ActivityName, temp);
}
return false;
}
you can also make your call by overriding onpagefinished method of webviewclient. I wanted to note this, since this is called when page finishes loading.
We have a requirement where we want multiple webview instance to be open from same application.
E.g Android app1 should open page1.html in first webview instance and page2.html in second webview instance. When page2.html is started page1.html webview should go in background.
Is this possible in Android? Is yes, Can you please provide sample code or link which talks in details as how this requirement can be achieved.
public void callwebtwo(){
loadweb = (WebView) findViewById(R.id.loadweb);
loadweb2 = (WebView) findViewById(R.id.loadweb2);
loadweb2.getSettings().setJavaScriptEnabled(true);
loadweb.getSettings().setJavaScriptEnabled(true);
loadweb2.loadUrl("http://www.google.co.in/");
loadweb.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("shouldOverrideUrlLoading", url.toString());
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.i("onPageFinished", url.toString());
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("onPageStarted", url.toString());
super.onPageStarted(view, url, favicon);
}
}
});
loadweb2.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("shouldOverrideUrlLoading", url.toString());
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.i("onPageFinished", url.toString());
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("onPageStarted", url.toString());
super.onPageStarted(view, url, favicon);
}
});
}
instance of the both web is different so it will work and perform seperatly..
i gvn ans whtever i cn understand frm ur quest.
Yes you can do it in android.
Create seperate activities for both and when you load page2 in second webview instance then obvisouly page1 webview instance will be in back of second and when you press back from page2 then page1 will be infront.