devdreamer
devdreamer

Reputation: 19

(Android Studio) Progress Dialogue doesn't disappear, when page finished loading

I'm building a WebView Application for my blog. Everything is running fine, except one thing. The ProgressDialog which appears on the screen while the Homepage is loading, doesn't disappear. Even after the webpage is finished loading.

However, it does go away after tapping the screen twice...........any help would be highly appreciated.

I'm pretty new to Android development so, a little explanation would be helpful with the answer for learning.

Ps Im using

minSdkVersion 21
targetSdkVersion 26

my Java code is:

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebView webView = new WebView(this);
        webView.setClickable(true);
        webView.setFocusableInTouchMode(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com");
        WebClientClass webViewClient = new WebClientClass();
        webView.setWebViewClient(webViewClient);

        setContentView(webView);
    }

    public class WebClientClass extends WebViewClient {
        ProgressDialog pd = null;

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

            pd = new ProgressDialog(MainActivity.this);
            pd.setTitle("Please wait");
            pd.setMessage("Page is loading..");
            pd.show();
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            pd.dismiss();
        }
    }


}

my xml code is:

<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"
    tools:context=".MainActivity">

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webview"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

My App Manifest code is:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.notherstore.notherrstore">

    <uses-permission android:name="android.permission.INTERNET" />

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >

            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

Upvotes: 0

Views: 144

Answers (2)

Igor Fridman
Igor Fridman

Reputation: 1297

onPageStarted() is called several times, so your ProgressDialog is staring several times as well. From what I found on this post: https://stackoverflow.com/a/25547544/5223744

If the url is OK after onPageStarted() method starts onPageFinished(), but if the url is redirected after onPageStarted() starts shouldOverrideUrlLoading and then onPageFinished().

Just place a breakPoint in your onPageStarted() and you will see the occurrence.

You can set a boolean which will check if onPageStarted() isFirstTime, or you can use the code below:

Just change your MainActivity class like below:

public class MainActivity extends AppCompatActivity {

    private ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebView webView = new WebView(this);
        webView.setClickable(true);
        webView.setFocusableInTouchMode(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com");
        WebClientClass webViewClient = new WebClientClass();
        webView.setWebViewClient(webViewClient);

        setContentView(webView);
        pd = new ProgressDialog(MainActivity.this);
        pd.setTitle("Please wait");
        pd.setMessage("Page is loading..");
        pd.show();
    }

    public class WebClientClass extends WebViewClient {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);


        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            pd.dismiss();
        }
    }

}

Upvotes: 1

Cagri Yalcin
Cagri Yalcin

Reputation: 402

If you want to keep progress dialog until page load finished, simply set dialog's cancelable false, so:

    pd.setCancelable(false);

Upvotes: 0

Related Questions