Can anyone help me when I add this ProgressDialog to my Webview app its work fine but when the page is loaded the progress dialog is not dismissed can anyone help me I am using this code in Fragment
public static ProgressDialog progressDialog;
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
swipeRefreshLayout.setRefreshing(true);
progressDialog.show(getActivity(), "Loading...", "Please wait YouTube is Loading...");
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
getActivity().setTitle(view.getTitle());
if(progressDialog!=null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
super.onPageFinished(view, url);
}
});
Edited code which is working for me now thanks to everyone who help me
public static ProgressDialog progressDialog;
//Under onCreate View
progressDialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait YouTube is Loading...");
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
swipeRefreshLayout.setRefreshing(true);
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
getActivity().setTitle(view.getTitle());
if(progressDialog!=null && progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog.hide();
}
super.onPageFinished(view, url);
}
});
Make sure that if(progressDialog!=null && progressDialog.isShowing()) this condition returns true. Put a break point there and make sure they both are true.
try
progressDialog.hide()
Move below line out of onPageStarted() method, as it will be called for each underlying URL and it will keep on showing unless every css and js files are loaded
progressDialog.show(getActivity(), "Loading...", "Please wait YouTube is Loading...");
Can you please try this code
progressDialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait YouTube is Loading...");
Hope it helps
Related
I created a webview and while the page is loading, there will be a ProgressDialog. Well, until this point all works.
But now I want that the ProgressDialog is dismiss after loading the url.
This doesn't work.
My Code:
private WebView webViewNews;
private ProgressDialog progressDialogWebViewNews;
webViewNews = (WebView) findViewById(R.id.webViewNews);
WebSettings webSettings = webViewNews.getSettings();
webSettings.setJavaScriptEnabled(true);
webViewNews.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialogWebViewNews = new ProgressDialog(MainActivity.this);
progressDialogWebViewNews.setCancelable(false);
progressDialogWebViewNews.setTitle("Demo...");
progressDialogWebViewNews.setMessage("Demo");
progressDialogWebViewNews.show();
}
#Override
public void onPageFinished(WebView view, String url) {
progressDialogWebViewNews.dismiss();
super.onPageFinished(view, url);
}
});
webViewNews.loadUrl("http://www.demo.com");
When you pass a variable to an anonymous class, the variable automatically becomes final which stops it from changing. Try having the progress dialog as a member of the inner client class instead. This also is a better design and makes your code more maintainable.
class MyClient extends WebViewClient() {
private ProgressDialog progressDialogWebViewNews;
public MyClient(Context c){
progressDialogWebViewNews = new ProgressDialog(c);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressDialogWebViewNews.setCancelable(false);
progressDialogWebViewNews.setTitle("Demo...");
progressDialogWebViewNews.setMessage("Demo");
progressDialogWebViewNews.show();
}
#Override
public void onPageFinished(WebView view, String url) {
progressDialogWebViewNews.dismiss();
super.onPageFinished(view, url);
}
}
webViewNews.setWebViewClient(new MyClient(MainActivity.this));
Delete super.onPageFinished(view, url),Then first check if progress dialog is showing before you dismiss it in your callback method.
If (progressDialogWebViewNews.showing){
progressDialogWebViewNews.dismiss();
}
Maybe you should create a simple WebView object:
WebView webView = (WebView) getActivity().findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("yourURL");
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading...");
progressDialog.show();
progressDialog.setCancelable(false);
webView.setWebViewClient(new WebViewClient()
{
public void onPageFinished(WebView view, String url)
{
progressDialog.dismiss();
}
});
It works for me.
I used code below to show progress bar, but it's not dismissing automatically (But when i touch on screen it dismiss)
N.B: Progress bar dismiss only home page in my webview. not any other page of posts. Please suggest me how to dismiss the progress bar dialogue automatically without touching it.
wv.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
wv.loadUrl(mypage_error);}
//ProgressDialogue
ProgressDialog pd = null;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd=new ProgressDialog(MainActivity.this);
pd.setTitle("Please Wait..");
pd.setMessage("Loading...");
pd.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
super.onPageFinished(view, url);
}
Don't allow the Progressbar to dismiss ,when you touch,set it like below
pd=new ProgressDialog(MainActivity.this);
pd.setTitle("Please Wait..");
pd.setMessage("Loading...");
pd.setCancelable(true);
pd.show();
//ProgressDialogue
final ProgressDialog pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Please Wait..");
pd.setMessage("Loading...");
wv.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
wv.loadUrl(mypage_error);}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
super.onPageFinished(view, url);
}
I want to hide the progress bar on webview as and when webview started rendering the content. I tried webview.getProgress() to get the current progress of webview. It returns progress in the range between 0-100. But I couldn't conclude that at which range actually webview starts render the content.
I would like to know that, Is there any method to identify the start of rendering the content in webview android.
Your help is much appreciated.
Thank you
You can try to add WebViewClient() and in callback methods hide progress.
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
});
Found the solution after a lot of searching! There is a "onPageCommitVisible" method in webViewClient which does exactly this, displays the progressbar until the page renders and the disappears.
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageCommitVisible(WebView view, String url) {
super.onPageCommitVisible(view, url);
mProgressBar.setVisibility(View.INVISIBLE);
}
});
Hope it helps :D
final ProgressDialog pDialog = new ProgressDialog(context);
pDialog.setMessage("Please wait....");
pDialog.setCancelable(false);
pDialog.show();
String url = "url";
final Activity activity = (Activity) context;
pdfView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setProgress(progress * 1000);
pDialog.incrementProgressBy(progress);
if(progress == 100 && pDialog.isShowing())
pDialog.dismiss();
}
});
Dialog will dismiss when webview start rendering the content.
class MyWebViewClient extends WebViewClient{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
}
class MYWebViewChromeClient extends WebChromeClient{
#Override
public void onProgressChanged(WebView view, int newProgress) {
//When the schedule is equal to 100 of the time, that is, the load is completed, you can hide the progress bar
super.onProgressChanged(view, newProgress);
}
}
I am new to android.I am using progress dialog in my app but it could not stop.In my app i am using webview.In first activity I have connect button.In second activity connect to facebook code is available.In second activity code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook);
wvFacebook.setWebViewClient(new MyWebViewClient());
wvFacebook.loadUrl(strFacebook);
}
private class MyWebViewClient extends WebViewClient
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
prDialog = ProgressDialog.show(getParent(), "In progress", "Loading, please wait...");
}
#Override
public void onPageFinished(WebView view, String url) {
Log.e("status","calling....");
super.onPageFinished(view, url);
prDialog.dismiss();
Log.e("status","calling next......");
}
}
when connect button clicked the second activity will be executed and getting facebook page.But progress dialog could not stop.
Try like this
First create dialog in your onCreate()
ProgressDialog progressDialog =new ProgressDialog(getApplicationContext());
progressDialog.setMessage("Please Wait");
Then show the dialog in onPageStarted() method.
progressDialog .show();
Then dismiss it in your onPageFinished() method
progressDialog.cancel();
You should not create Dialog in onPageStart, when loading a web page, onPageStart() could be called multi times.
In you onPageStart, you create a new Dialog without closing the old one, so the old dialog will always show.
To solve this you have many choices:
As lmran said, create the dialog only once in onCreate(). Show it in onPageStarted and dismiss in onPageFinished.
Before create a new Dialog, dismiss the old one.
just this code used and try it out,
#Override
public void onPageFinished(WebView view, String url) {
Log.e("status","calling....");
super.onPageFinished(view, url);
if (prDialog != null || prDialog.isShowing())
prDialog.dismiss();
Log.e("status","calling next......");
}
#Override
public void onPageFinished(WebView view, String url) {
try{
if (prDialog.isShowing()|| prDialog!= null) {
prDialog.dismiss();
prDialog= null; /*** Add ***/
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}
You may also visit the following link to get a complete detail...
http://androiddubd.blogspot.com/2014/07/how-to-show-progress-dialog-or-loader.html
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();
}
}