Pandit Ji
Pandit Ji

Reputation: 23

Progress Dialog not closing in WebView

I am developing a Android Web Application.. I have created some code but facing one problem. Problem is that... Progress Dialog is still after loading Homepage... but when I click on page it gone... this problem with Homepage only... not inside.. If I open another link from page then working perfectly.. I have not tested on my Phone.. I have tested it on Emulator Only... and please take a look on the code... it there is any change required... please suggest.. This is my .Java file

package com.aarcreationz.bhajanmala;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity 
{
    WebView mWeb;
    ProgressDialog mProgress;


    @SuppressLint("SetJavaScriptEnabled") 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mWeb = new WebView(this);
        mWeb.setClickable(true);
        mWeb.setFocusableInTouchMode(true);
        setContentView(mWeb);
        WebSettings settings = mWeb.getSettings();
        settings.setJavaScriptEnabled(true);


        mWeb.setWebViewClient(new WebViewClient() {
            ProgressDialog mProgress = null;

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }


            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                mProgress = new ProgressDialog(MainActivity.this);
                mProgress.setTitle("Please Wait!");
                mProgress.setMessage("Loading...");
                mProgress.show();

            }

            public void onPageFinished(WebView view, String url) {
                if(mProgress.isShowing()) {
                    mProgress.hide();
                    mProgress.dismiss();
                }
            }
        });

        mWeb.loadUrl("http://www.aarcreationz.com/mala");
    }
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWeb.canGoBack())
        {
            mWeb.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

This is my layout file

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${packageName}.${activityClass}" >   
</WebView>

Upvotes: 0

Views: 462

Answers (1)

Illegal Argument
Illegal Argument

Reputation: 10338

You are missing the @override annotation in your mWeb.setWebViewClient(new WebViewClient() code. Due to this functions like onPageStarted is acting as normal functions. Check this link to see how to set up properly.

Upvotes: 1

Related Questions