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
Related
I have a WebView in my Android app which displays a web page with a specific URL. The URL is a signal to the app to close the WebView and perform other actions.
I'd like to display a ProgressDialog over the WebView while the app loads the functions required by the URL. However when the ProgressDialog appears, it appears dimmed, as if there is an overlay superimposed on top of it. Displaying an AlertDialog appears as normal. Can anyone advise why the PD appears dimmed?
private class WebView extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, final String url) {
if (url.contains("my/special/url")) {
// ProgressDialog message appears dimmed
ProgressDialog progress;
progress = new ProgressDialog(view.getContext());
progress.setMessage("Loading...");
progress.show();
progress.setCancelable(false);
progress.setCanceledOnTouchOutside(false);
// AlertDialogs appear as normal
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog;
builder.setMessage("Loading");
}
return true;
}
}
In your code your private Inner class name is WebViewthat need to change.
It could be MyWebView or anything else. But should not WebView.
Because,
#Override
public boolean shouldOverrideUrlLoading(WebView view, final String url) {
}
In this override method there is a paramiter WebView that is build in class in android from android.webkit package.
If you keep your class name WebView that means you not overriding method form WebViewClient shouldOverrideUrlLoadingand it will not work.
Then
You can override onPageStarted to show your progress dialog
because it call on loading the page....
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
if (url.contains("my/special/url")) {
// ProgressDialog message appears dimmed
ProgressDialog progress;
progress = new ProgressDialog(view.getContext());
progress.setMessage("Loading...");
progress.show();
progress.setCancelable(false);
progress.setCanceledOnTouchOutside(false);
// AlertDialogs appear as normal
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog;
builder.setMessage("Loading..");
}
}
You must ensure that if (url.contains("my/special/url")) is true. otherwise it never execute...because if(false) never execute ... in this case else part will execute if have..
For showing progress in webview, WebViewChromeClient is used. Try to implement webview using below example. Hope it solves your problem.
public class webView extends AppCompatActivity {
private String mUrl;
public WebView mWebView;
// public ProgressBar progressBar;
private ProgressBar progressBar;//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
Log.e("Webview class", "I'm in web view");
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
//set progress bar max limit
progressBar.setMax(100);
//get the url from the intent of this activity called from the fragments
mUrl = getIntent().getExtras().getString("url");
// set the title of webview to be th url
setTitle(mUrl);
mWebView = (WebView) findViewById(R.id.webview);
//mWebView = new WebView(this);
mWebView.setWebViewClient(new myWebViewClient());
mWebView.setWebChromeClient(new WebChromeClientDemo());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(mUrl);
//setContentView(mWebView);
}
private class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
}
private class WebChromeClientDemo extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
}
}
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 have a webview in my android application which renders html pages from a local folder, Now I want to show progress dialog when navigating from one html page to another but the progress dialog which I am using isn't showing up. Here is my code snippet:
#JavascriptInterface
public void save(String respString, boolean ifEndNode) throws JSONException {
ProgressDialog progDialog = null;
try {
if(ifEndNode){
//start loader
progDialog = new ProgressDialog(webView.getContext());
progDialog.setMessage("Saving survey, Please DON'T close the Application!! ");
progDialog.setCanceledOnTouchOutside(false);
progDialog.setCancelable(false);
progDialog.show();
}
//some code here
//...
}
catch (Exception e)
{
Logger.e(context,"exception", "jsonObjectexception");
}
finally {
if(progDialog != null && progDialog.isShowing())
progDialog.dismiss();
}
Can anyone suggest what the problem could be?
You can use WebView with ProgressDialog this way. This is nice and simple approch.
private WebView webView;
ProgressDialog prDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_news);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
webView = (WebView) findViewById(R.id.wv_news);
webView.setWebViewClient(new MyWebViewClient());
String url = "http://google.com/";
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.loadUrl(url);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
prDialog = new ProgressDialog(NewsActivity.this);
prDialog.setMessage("Please wait ...");
prDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(prDialog!=null){
prDialog.dismiss();
}
}
}
I have used progress bar to indicate page is loading in android webview. But the Progress Bar is showing for ever even after the page has been loaded completely. I used logcat for debugging which shows onPageFinished function is called.
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
_dialog =ProgressDialog.show(UmlScreen.this, "", "Please wait...");
super.onPageStarted(view, url, favicon);
_dialog.setCanceledOnTouchOutside(true);
Log.d("LOGCAT", "started");
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if (_dialog != null || _dialog.isShowing()) {
_dialog.dismiss();
Log.d("LOGCAT", "finished");
}
}
Try initialization of _dialog outside WebViewClient such as in OnCreateView method
Because the page is not finished. May be the page have a lot of images so it is taking time to download them.
you can check it using break point. debug you code and put break point at super.onPageFinished(view, url);
I can give you some dummy codes.
webview.load(url);
dialog.show();
public void onPageFinished(WebView view, String url) {
//....
dialog.dismiss();
}
#Override
public void onPageFinished(WebView view, String url) {
try{
if (prDialog.isShowing()|| prDialog!= null) {
prDialog.dismiss();
prDialog= null; /*** Add ***/
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}
Or visit...
https://stackoverflow.com/a/31981161/3857542
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();
}
}