Skip to content Skip to sidebar Skip to footer

Progress Bar In Android Webview

I am trying to add a progress bar in android webview using WebViewClient. It displays the progress bar, a text (loading) and rest of the page is just a blank page. I want to keep

Solution 1:

This might Help, Source: ProgressBar with WebView

You can trace the Progress Staus by the getProgress method in webview class.

Initialize the progress status

privateint mProgressStatus = 0;

then the AsyncTask for loading like this:

privateclassTask_News_ArticleViewextendsAsyncTask<Void, Void, Void> {
    privatefinal ProgressDialog dialog = new ProgressDialog(
            your_class.this);

    // can use UI thread hereprotected void onPreExecute() {
        this.dialog.setMessage("Loading...");
        this.dialog.setCancelable(false);
        this.dialog.show();
    }

    @OverrideprotectedVoid doInBackground(Void... params) {
        try {
            while (mProgressStatus < 100) {
                mProgressStatus = webview.getProgress();

            }
        } catch (Exception e) {

        }
        returnnull;

    }

    protected void onPostExecute(Void result) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }
    }
}

Solution 2:

publicclassAboutextendsAppCompatActivity {

    WebView myWebView;
    ProgressBar progressBar;
    FrameLayout frameLayout;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("About CreativeGraphy");
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        progressBar = findViewById(R.id.progress_bar);
        frameLayout = findViewById(R.id.frame_loading);
        progressBar.setMax(100);

        myWebView = findViewById(R.id.webview);
        myWebView.loadUrl("http://www.google.co.in/");
        myWebView.setWebViewClient(newHelpClient());

        myWebView.setWebChromeClient(newWebChromeClient() {
            @OverridepublicvoidonProgressChanged(WebView view, int newProgress) {
                frameLayout.setVisibility(View.VISIBLE);
                progressBar.setProgress(newProgress);
                setTitle("Loading....");
                if (newProgress == 100) {
                    frameLayout.setVisibility(View.GONE);
                    //setTitle(View.getTitle());
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setVerticalScrollBarEnabled(false);
        progressBar.setProgress(0);
    }

    @OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }//close activity when click back buttonreturnsuper.onOptionsItemSelected(item);
    }

    publicvoidonBackPressed() {
        if (myWebView.canGoBack()) {
            myWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @OverridepublicbooleanonKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if (myWebView.canGoBack()) {
                myWebView.goBack();
                returntrue;
            }
        }
        returnsuper.onKeyDown(keyCode, event);
    }

    privateclassHelpClientextendsWebViewClient {
        @OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            frameLayout.setVisibility(View.VISIBLE);
            returntrue;
        }
    }
}

try this it has loading

layout

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"tools:context=".About"><android.support.design.widget.AppBarLayoutandroid:id="@+id/app_bar"android:layout_width="match_parent"android:layout_height="wrap_content"android:theme="@style/AppTheme.AppBarOverlay"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"app:popupTheme="@style/AppTheme.PopupOverlay"app:theme="@style/ToolbarColoredBackArrow" /></android.support.design.widget.AppBarLayout><FrameLayoutandroid:id="@+id/frame_loading"android:layout_width="match_parent"android:layout_height="3dp"android:layout_below="@id/app_bar"android:background="@android:color/transparent"><ProgressBarandroid:id="@+id/progress_bar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="match_parent"android:layout_height="8dp"android:layout_gravity="top"android:layout_marginTop="-3dp"android:background="@android:color/transparent"android:progress="20"android:progressDrawable="@drawable/progress_cyclic" /></FrameLayout><WebViewandroid:id="@+id/webview"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@id/frame_loading" /></RelativeLayout>

try using 2 webview if its 100% loaded visible it other one gone first will stay while other loads when it is loaded second web view starts loading

Post a Comment for "Progress Bar In Android Webview"