ProgressDialog over a WebView appears dimmed - android

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);
}
}
}

Related

Dismiss ProgressDialog after WebView loads url does not work

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.

Progress Dialog not showing in webview

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();
}
}
}

Dismiss ProgressDialog after WebView has shown

How can I use ProgressDialog to wait while WebView is loading?
Sometimes myText has a huge text and some old devices can't load WebView fast. I want to show ProgressDialog while WebView is loading and when it's already shown I want to dismiss the ProgressDialog.
I have:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Please wait...");
dialog.setIndeterminate(true) ;
dialog.show();
String myText = "some text";
WebView myWebView = (WebView)findViewById(R.id.webView);
myWebView.loadDataWithBaseURL("", myText, "text/html", "UTF-8", "");
dialog.dismiss();
It doesn't show any ProgressDialog.
If I don't use dismiss, I always see my ProgressDialog.
Use AsyncTask.
// ..onCreate code come here...
Button button = (Button) findViewById(R.id.button);
bt.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
new LongOperation().execute(); // here calling LongOperation class to do `AsyncTask`.
}
});
private class LongOperation extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(MainActivity.class);
dialog.setTitle("Please wait...");
dialog.setIndeterminate(true) ;
dialog.show();
}
#Override
protected String doInBackground(Void... params) {
// background operation comes here..
}
#Override
protected void onPostExecute(String result) {
// close progressDialog here by calling
dialog.dismiss();
}
}
Put this code in your current Java class which contains web view..
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if(dialog.isShowing() && dialog != null) {
dialog.dismiss();
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!dialog.isShowing() && dialog != null) {
dialog.show();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
}
And set this
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
below you initialized your webview
just try this,
webView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url)
{
progDailog.dismiss();
}
});

progress dialog could not stop

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

How to use ProgressDialog with webview in a fragment

Now im trying to show a page in webview, it takes some time to load, meanwhile i want to show a progressDialog. The best i could do was the code below, but the problem is that im doing it in onCreateView, so the progressDiealog only shows until the view is created, which is not what i want. I know that i have to use it in onActivityCreated , but im not sure how to do that , thanks all in advance
public class someFragment extends SherlockFragment {
Context context;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
View v = inflater.inflate(R.layout.some_layout, group, false);
final ProgressDialog pd = ProgressDialog.show(getActivity(), "", "Loading", true);
final WebView myWebView = (WebView) v.findViewById(R.id.webViewsome);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
}
});
myWebView.getSettings().setPluginsEnabled(true);
myWebView.getSettings().setBuiltInZoomControls(false);
myWebView.getSettings().setSupportZoom(false);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.setInitialScale(25);
myWebView.getSettings().setDefaultZoom(ZoomDensity.FAR);
myWebView.loadUrl("http://www.test.com/test-0.jpg");
return v;
}
}
Try this in SomeFragment:
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
//remove your progressdialog here
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//show your progressdialog here
super.onPageStarted(view, url, favicon);
}
}
then:
webView.setWebViewClient(new MyWebViewClient());
One thing you can do is, in onCreateView start a AsyncTask with a progressDialog in it. In onPageFinished(after web page completely loaded) of webView, cancel that task. Hope its useful.

Categories

Resources