App Crashes when Dismissing ProgressDialog - android

I am using ProgressDialog in my WebView app.
In onPageStarted, I show the dialog but when using pd.dismiss in onPageFinished, the app crashes.
web.setWebViewClient(new WebViewClient() {
//Opens all the Clicked Links in App itself.. (No Opera/Browser/Chrome/Uc popup)
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
//Shows a Dialog when there is an Error
#Override
public void onReceivedError(WebView view, int errorcode,String description, String fallingUrl) {
Toast.makeText(getActivity(),"NetWork Error", Toast.LENGTH_SHORT).show();
}
//Showing ProgressBar when Page is Loading
#Override
public void onPageStarted(WebView view,String url , Bitmap favicon){
ProgressDialog pd = new ProgressDialog(getActivity());
pd.setMessage("Loading...");
pd.show();
}
//Hiding ProgressBar when Loading Finished
public void onPageFinished(WebView view,String Url){
pd.dismiss();
}
});

it happens for nullPointerException. You need to make the ProgressDialog as an instance filed of that class and initialize it onCreate. Like the following:
private ProgressDialog pd;
#override
public void onCreate () {
pd = new ProgressDialog(getActivity());
pd.setMessage("Loading...");
}
now when you need to show that just call show() method on that pd progressDialog and you can dismiss at any time by calling dismiss() method.

ProgressDialog pd = new ProgressDialog(getActivity());
web.setWebViewClient(new WebViewClient() {
//Opens all the Clicked Links in App itself.. (No Opera/Browser/Chrome/Uc popup)
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
//Shows a Dialog when there is an Error
#Override
public void onReceivedError(WebView view, int errorcode,String description, String fallingUrl) {
Toast.makeText(getActivity(),"NetWork Error", Toast.LENGTH_SHORT).show();
}
//Showing ProgressBar when Page is Loading
#Override
public void onPageStarted(WebView view,String url , Bitmap favicon){
pd.setMessage("Loading...");
pd.show();
}
//Hiding ProgressBar when Loading Finished
public void onPageFinished(WebView view,String Url){
pd.dismiss();
}
});
It works for me.

Try this:
//Declare it globally
LinearLayout progressLayout;
In the onCreate():
progressLayout = (LinearLayout) findViewById(R.id.ll_refreshing_progress);
wv_tandc.setWebViewClient(new MyWebViewClient());
wv_tandc.loadUrl(getString(R.string.terms_and_conditions_url));
Implement the WebViewClient :
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressLayout.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
System.out.println("on finish");
progressLayout.setVisibility(View.GONE);
}
}
Write the below code in your xml,where you have defined your WebView:
<LinearLayout
android:id="#+id/ll_refreshing_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
android:padding="5dp"
android:orientation="vertical">
<ProgressBar
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sf_refreshing"/>
</LinearLayout>
You do not need to use progressdialog,instead you can use custom layout.

Based on this, I assume pd is a member variable.
//Hiding ProgressBar when Loading Finished
public void onPageFinished(WebView view,String Url){
pd.dismiss();
}
However, you seem to think this is assigning that same pd variable, but it isn't. See variable shadowing
//Showing ProgressBar when Page is Loading
#Override
public void onPageStarted(WebView view,String url , Bitmap favicon){
ProgressDialog pd = new ProgressDialog(getActivity());
pd.setMessage("Loading...");
pd.show();
}
Remove ProgressDialog from ProgressDialog pd =, so just pd = new ...

Declare ProgressDialog object as member variable

public void onPageFinished(WebView view,String Url){
if(pd ! =null)
pd.dismiss();
}

Just change
#Override
public void onPageStarted(WebView view,String url , Bitmap favicon){
pd = new ProgressDialog(getActivity());
pd.setMessage("Loading...");
pd.show();
}
//Hiding ProgressBar when Loading Finished
public void onPageFinished(WebView view,String Url){
pd.dismiss();
}
and declare dialog object as
private ProgressDialog pd;

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.

Progressbar dialogue don't dismiss after page loade finished

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

How to show ProgressDialog on every click on Link in WebView

I am using webview with ProgressDialog. My issue is that on the Initial Launch, the progressDialog appears abd load properly but whenever I click a link (post links) inside webview, the progress is not showing. Here's the code -
public class Xyz extends WebViewClient
{
public void onLoadResource (WebView view , String url ) {
if (progressDialog == null ) {
progressDialog = new ProgressDialog(getActivity());
progressDialog . setMessage( "Loading..." );
progressDialog.setIndeterminate(true);
progressDialog . show();
}
}
public void onPageFinished(WebView view , String url ) {
if (progressDialog . isShowing()) {
progressDialog . dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
You are overriding the wrong method to achieve your goal. I've put up an Activity here which works as intended.
I'm using onPageStarted() instead of onLoadResource().
Activity MainActivity.java:
public class MainActivity extends AppCompatActivity {
ProgressDialog progressDialog;
WebViewClient mClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mClient = new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (progressDialog == null) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.setIndeterminate(true);
}
if(!progressDialog.isShowing()) {
progressDialog.show();
}
}
public void onLoadResource(WebView view, String url) {
}
public void onPageFinished(WebView view, String url) {
if (progressDialog!=null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
};
WebView webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(mClient);
webView.loadUrl("http://www.google.com");
}
}
Layout activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.prasad.myapplication.MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Try to track the webview load progress using webChoromeClient like this:
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);
} else {
progressBar.setVisibility(View.VISIBLE);
}
}
});
and replace your webview client as follwing
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

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

Categories

Resources