I know how to get notified when it finishes loading a web page, but is there any way to know when it starts loading a new one that is initiated from a link from the original web page?
The reason that i want it is to make a ProgressBar visible whenever a page starts loading, and make it invisible whenever it finishes.
UPDATE: What I'm asking is if it's possible to know when a new page starts loading a page. Although from the link I found onPageStarted, and it works.
Check with the following
webView.setWebViewClient(new WebViewClient() {
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource (WebView view, String url) {
//Show loader on url load
}
public void onPageFinished(WebView view, String url) {
//cancel loader complted
}
});
// Set Javascript enabled on webview
webView.getSettings().setJavaScriptEnabled(true);
To listen for a web page starting loading you should override onPageStarted, to listen for finishing you should override onPageFinished
and just to make it more complete, listen for error with onReceivedError
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
// runs when a page starts loading
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// page finishes loading
progressBar.setVisibility(View.GONE);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
// runs when there's a failure in loading page
progressBar.setVisibility(View.GONE);
Toast.makeText(context, "Failure on loading web page", Toast.LENGTH_SHORT).show();
}
});
Related
I have made a simple android webview app. Everything is working fine only my progressbar (loader) not showing when app opened. Rest of the pages when user navigate to another link then progressbar showing.
MainActivity.java file
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
...
}
#Override
public void onPageStarted(WebView view, String url) {
super.onPageStarted(view, url);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
});
My problem is onPageStarted method. When I remove this then every page navigation display progressbar (except first time when app opened) but once I place onPageStarted method as above then android studio display an error like
method does not override method from its superclass
I am new in android and apology for this question if it is violet any policy. Need help.
Your onPageStarted() override method do not have all the required parameters of parents class. It should be as shown below
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
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.
I am working with android app for the web view. It loads the webpage but i cant make more actions on the page using my app. when I am choosing a hyperlink from the page it shows a popup"complete the action using chrome,opera etc.." how can I use this without such browsers..I nedd to browse web contents using my app. how it possible??
I used the code
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
please help me since I am new to android and thank you.
wv.loadUrl(url);
wv.setWebViewClient(new MyWebViewClient());
Your WebViewCLient class would look like this:
class MyWebViewClient extends WebViewClient {
#Override
// show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webProg.setVisibility(View.GONE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
I want to trigger a function after webview's finished loading a webpage and received no error. According to my experiment, the onPageFinished() will be triggered several times and in most of the cases, it will be executed before onReceivedError().
So how can I know that the webpage is loaded and no error is received?
You can set the WebViewClient in the WebView and Override its Callback methods which will let you know when the webpage has finished loading the page. For example
webView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
if(progess!=null){
progess.dismiss();
}
progess = ProgressDialog.show(LoadUrl.this, "", "Loading please wait...");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
if (Uri.parse(url).getHost().equals("callback")) {
// This is my web site, so do not override; let my WebView load the page
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url){
super.onPageFinished(view, url);
// Add YOUR CODE HERE
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(LoadUrl.this, "Sorry! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
handler.proceed();
}
}
Here onPageFinished(WebView view, String url) will be called when webView has finished loading the current Page.
Hope it helps :)
My question is different from this one guys..
I wany my progress dialog start when page load starts and end when the page load finished in my webview. My problem is the progress dialog starts and never get dismissed.I have set break points it shows that the progress dialog starts and get dismissed many times then it starts and not get dismissed even after page load completed. My question is why the onPageStarted getting executed many time for a single page loading?
and why onPageFinished not called after completion of page load?
myWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
myWebView.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(myWebView, url, favicon);
Log.d("mytag","Page Loading Started");
//myURLProgressDialog= ProgressDialog.show(WebviewExampleActivity.this, "Page Loading", "Wait for a moment...");
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d("mytag","Page Loading Finished!");
super.onPageFinished(myWebView, url);
//myURLProgressDialog.dismiss();
}
});
My self tagged filtered Log is Like this for loading single page:
10-06 10:32:49.298: DEBUG/mytag(508): Page Loading Started
10-06 10:32:49.998: DEBUG/mytag(508): Page Loading Started
10-06 10:32:50.048: DEBUG/mytag(508): Page Loading Finished!
10-06 10:32:50.048: DEBUG/mytag(508): Page Loading Started
10-06 10:33:00.898: DEBUG/mytag(508): Page Loading Finished!
When I am clicking link on already loaded page it works fine. Here is
Log:
10-06 10:59:25.098: DEBUG/mytag(543): Page Loading Started
10-06 10:59:30.889: DEBUG/mytag(543): Page Loading Finished!
Check whether it is already open with .isShowing().
final ProgressDialog mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading...");
browser.setWebViewClient(new myViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!mProgressDialog.isShowing())
{
mProgressDialog.show();
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
}
});
If you have ajax calls in the page being loaded, onPageFinished() will be called only when those calls finish.
Better to make those calls with a window.setTimeout(). In that case, UI thread will not be blocked on those calls and onPageFinished() will be called as soon as the main page loads.
Here is a solution. Instead of a loading dialog, i use another webview as splash-screen, but you can change it easily.
The trick is, to look if there is a new "onpagestart" right after the "onpagefinished". If this is the case, don't close the loading and wait for the next "onpagefinished".
myWebView.setWebViewClient(new WebViewClient() {
boolean loadingFinished = true;
boolean redirect = false;
long last_page_start;
long now;
// Load the url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.loadUrl(url);
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("p","pagestart");
loadingFinished = false;
last_page_start = System.nanoTime();
show_splash();
}
// When finish loading page
public void onPageFinished(WebView view, String url) {
Log.i("p","pagefinish");
if(!redirect){
loadingFinished = true;
}
//call remove_splash in 500 miSec
if(loadingFinished && !redirect){
now = System.nanoTime();
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
remove_splash();
}
},
500);
} else{
redirect = false;
}
}
private void show_splash() {
if(myWebView.getVisibility() == View.VISIBLE) {
myWebView.setVisibility(View.GONE);
myWebView_splash.setVisibility(View.VISIBLE);
}
}
//if a new "page start" was fired dont remove splash screen
private void remove_splash() {
if (last_page_start < now) {
myWebView.setVisibility(View.VISIBLE);
myWebView_splash.setVisibility(View.GONE);
}
}
});
Sometimes happens that when the initial page loads, some script makes a redirect (because it needed a session ID, or something that would make the page load again).
You can check if a different page (or the same one with additional parameters) is loading by logging the url parameter.
I guess that if a new load request is made before the first page finishes loading, then the OnPageFinished method won't be called for that first page.
When a page submit on webview app: onPageStarted Fired, but onPageFinished not Fired so follow code below fixed and exactly
ProgressDialog pd = null;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(pd == null || !pd.isShowing()) {
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Please wait");
pd.setMessage("App is loading...");
pd.show();
}
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (pd != null) {
pd.dismiss();
}
}