onPageStarted and onPageFinished don't work - java

Here is my code snippet:
webView = (WebView) browserView.findViewById(R.id.webView);
webView.loadUrl(webUrl);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("some_domain")) {
webView.loadUrl(url);
Toast.makeText(context, "Loaded", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
});
Methods onPageStarted and onPageFinished are executed only the first time I click a link. Then, these methods don't work. What could be the problem?
Thanks.

Can you try changing your shouldOverrideUrlLoading method to
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.VISIBLE);
webView.loadUrl(url);
return true;
}

Related

Progress Dialog Does Not Dismiss After Loading Webview

Progress Dialog Does Not Dismiss After Loading Webview Is Goes On loading After Webview Is Loaded.
public class MainActivity extends AppCompatActivity {
private WebView webView;
ProgressDialog pd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pd = ProgressDialog.show(MainActivity.this, "Progress Dialog", "Loading...");
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://kkgupta.mmiswebtech.com");
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("file:///android_asset/error.html");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});
}
class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
// This method is used to detect back button
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
You'll need to add onPageFinished() to your MyWebClient and in it call dismiss() on your ProgressDialog:
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pd.dismiss();
}
use
pd.dismiss();
where u want ti dismiss Dialog.
Note: ProgressDialog is deprecated in API level 26. Better Use ProgressBar
Delete this code:
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("file:///android_asset/error.html");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});
}
change:
class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
To:
class myWebClient extends 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); if (pd.isShowing()) { pd.dismiss(); } }
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

Android - WebView canGoBack() always returns false?

Why does WebView canGoBack() always return false?
if (myWebView.canGoBack())
{ myWebView.goBack(); } //always false
onCreate:
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setBuiltInZoomControls(true);
myWebView.loadUrl( getString(R.string.base_url) );
myWebView.setWebViewClient(new MyWebViewClient(progress));
MyWebviewClient:
public class MyWebViewClient extends WebViewClient {
private ProgressBar progressBar;
public MyWebViewClient(ProgressBar progressBar) {
this.progressBar=progressBar;
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
share_btn.setVisibility(View.GONE);
back_btn.setVisibility(View.GONE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// TODO Auto-generated method stub
progressBar.setVisibility(View.GONE);
//hide loading image
findViewById(R.id.imageLogo).setVisibility(View.GONE);
//show webview
findViewById(R.id.websiteView).setVisibility(View.VISIBLE);
toolbar.setVisibility(View.VISIBLE);
currentURL = url.replace("mapp.","");
if(url.contains("article")) {
share_btn.setVisibility(View.VISIBLE);
back_btn.setVisibility(View.VISIBLE);
}
}
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!Global.IsInternetConnected(MainActivity.this))
Toast.makeText(getApplicationContext(), "No internet connection!", Toast.LENGTH_SHORT).show();
else
view.loadUrl(url);
return true;
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (!Global.IsInternetConnected(MainActivity.this))
Toast.makeText(getApplicationContext(), "No internet connection!", Toast.LENGTH_SHORT).show();
else {
view.loadUrl(request.getUrl().toString());
}
return true;
}
}
It's working fine on the emulator pixel_XL_API_25 but not working neither Samsung S6 nor Note3.
Finally I found the solution, The issue was in shouldOverrideUrlLoading
Please find below correct shouldOverrideUrlLoading function:
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!Global.IsInternetConnected(MainActivity.this)) {
Toast.makeText(getApplicationContext(), "No internet connection!", Toast.LENGTH_SHORT).show();
return true;
}
else
return super.shouldOverrideUrlLoading(view, url);
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (!Global.IsInternetConnected(MainActivity.this)) {
Toast.makeText(getApplicationContext(), "No internet connection!", Toast.LENGTH_SHORT).show();
return true;
}
else
return super.shouldOverrideUrlLoading(view, request);
}

Access the JSON response from webview of android

On Android, I have a webView. I'm loading a url on it. Firstly, I get a login page of Facebook and after filling login details it gives me a JSON Response on same webView.
I'm using this code to load that URL but now I don't know how to get that JSON Response in a string what I got on finished of that url.
WebViewClient yourWebClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
wb.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
};
wb = (WebView) findViewById(R.id.webview);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setSupportZoom(true);
wb.getSettings().setBuiltInZoomControls(true);
wb.setWebViewClient(yourWebClient);
wb.loadUrl("http://pqrs.abcde.com/facebook");
The format of JSON Response is:
{"status":"success","msg":"You have successfully logged in."}
Build a class called MyJavaScriptInterface. And MyJavaScriptInterface to WebView.
WebViewClient yourWebClient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
wb.loadUrl("javascript:HtmlViewer.showHTML" +
"('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
}
};
wb = (WebView) findViewById(R.id.webview);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setSupportZoom(true);
wb.getSettings().setBuiltInZoomControls(true);
wb.setWebViewClient(yourWebClient);
wb.loadUrl("http://pqrs.abcde.com/facebook");
wb.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");
class MyJavaScriptInterface {
private Context ctx;
MyJavaScriptInterface(Context ctx) {
this.ctx = ctx;
}
#JavascriptInterface
public void showHTML(String html) {
System.out.println(html);
}
}
Note: From you are required to remove window in wb.loadUrl("javascript:...");.
And #JavascriptInterface before "public void showHTML".
You can get content from showHtml(..) in html parameter.
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(webdata);
}
public class myWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}

Removing webview loader when loaded

I am trying to implement a webview loader, but once the page has loaded, the loader keeps showing:
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webcontent);
webView = (WebView) findViewById(R.id.webView);
final ProgressDialog progDailog = ProgressDialog.show(WebActivity.this, "Loading ","Please wait... ", true);
progDailog.setCancelable(false);
webView.setWebChromeClient(new WebChromeClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
}
});
//webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
webView.loadDataWithBaseURL("www.example.com", null, "text/html", "UTF-8", null);
webView.loadUrl("www.example.com");
}
I believe "onPageFinished" should remove the loader once the page has loaded?
Thanks
Replace this:
webView.setWebChromeClient(new WebChromeClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
}
});
by this:
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progDailog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progDailog.dismiss();
}
});
It worked for me.

How do i make my progress dialog dismiss after webview is loaded?

What do I need to my code to make the dialog dismiss() after the webview is loaded?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new homeClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
webview.loadUrl("http://google.com");
ProgressDialog pd = ProgressDialog.show(Home.this, "",
"Loading. Please wait...", true);
}
I've tried
public void onPageFinshed(WebView view, String url){
pd.dismiss();
}
Didn't work.
:o
webview.setWebViewClient(new homeClient()); homeClient()????
try this
...
...
...
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
webview.loadUrl("http://www.google.com");
}
Update::
this is a good example.
Android WebView and the Indeterminant Progress Solution
This is OK.
public class WordActivity extends Activity {
private WebView webview;
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Bundle objetbunble = this.getIntent().getExtras();
webview = (WebView) findViewById(R.id.webview);
final Activity activity = this;
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource (WebView view, String url) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Chargement en cours");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}
});
webview.loadUrl("http://www.example.com");
}
}
#Jorgesys this is not 100% accurate. If you have several iframes in a page you will have multiple onPageFinished (and onPageStarted). And if you have several redirects it may also fail. This approach i think solves all the problems:
boolean loadingFinished = true;
boolean redirect = false;
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
webView.loadUrl(urlNewString);
return true;
}
#Override
public void onPageStarted(WebView view, String url) {
loadingFinished = false;
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
}
#Override
public void onPageFinished(WebView view, String url) {
if(!redirect){
loadingFinished = true;
}
if(loadingFinished && !redirect){
//HIDE LOADING IT HAS FINISHED
} else{
redirect = false;
}
}
});
How are you accessing pd in onPageFinshed()? (And are you sure it's actually called when the page loads?)
In your onCreate(), try passing pd to your homeClient so that homeClient can take care of dismissing the dialog.
Your homeClient should look like this:
private class homeClient extends WebViewClient {
private ProgressDialog pd;
public homeClient(ProgressDialog pd) {
this.pd = pd;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished (WebView view, String url) {
if (pd.isShowing()) {
pd.dismiss();
}
}
}
In your onCreate():
ProgressDialog pd = ProgressDialog.show(Fbook.this, "",
"Loading. Please wait...", true);
webview.setWebViewClient(new homeClient(pd));
If you simply want to show Progress before webPage loading in WebView. You can simply request for Window Feature Progress like
getWindow().requestFeature(Window.FEATURE_PROGRESS);
before setContentView(R.layout.blahblah);
and show it progress in onProgressChanged like
final Activity context= this;
webview.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView webView, int progress)
{
activity.setProgress(progress * 1000);
}
});
And if you want to add you own ProgressDialog then use WebviewClient
webView.setWebViewClient(new WebViewClient() {
ProgressDialog rogressDialog ;
#Override
public void onPageStarted(WebView view, String url, Bitmap bitmap) {
progressDialog = ProgressDialog.show(context, "Loading...", "Please wait...");//where context = YourActivity.this;
super.onPageStarted(view, url, bitmap);
}
#Override
public void onPageFinished(WebView view, String url) {
progressDialog .dismiss();
super.onPageFinished(view, url);
}
});
webView.loadUrl(url);
wv1.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(Entertainment.this, description, Toast.LENGTH_SHORT).show();
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
progressDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
progressDialog.dismiss();
String webUrl = wv1.getUrl();
}
});
wv1.loadUrl(url);
The above code will show a progress dialogue for every incoming link you navigate to the in webview.

Categories