Saturday, January 23, 2016

android - Display ProgressBar while loading in WebView

MainActivity.java

package com.cfsuman.androidtutorials;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {
        private MainActivity mContext;
        private WebView mWebView;
        private ProgressBar mProgressBar;
        private Button mButton;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                // Get the activity
                mContext = MainActivity.this;

                // Get the widgets reference from XML layout
                mButton = findViewById(R.id.button);
                mWebView = findViewById(R.id.webView);
                mProgressBar = findViewById(R.id.progressBar);


                // Set a click listener for button widget
                mButton.setOnClickListener(view -> {
                        // Request to render the web page
                        renderWebPage("https://www.android--code.com/");
                });
        }



        // Custom method to render a web page
        protected void renderWebPage(String urlToRender){
                mWebView.setWebViewClient(new WebViewClient(){
                        @Override
                        public void onPageStarted(WebView view, String url,
                                                  Bitmap favicon){
                                // Do something on page loading started
                                // Visible the progressbar
                                mProgressBar.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onPageFinished(WebView view, String url){
                                // Do something when page loading finished
                                showToast("Page loaded");
                        }

                });


                mWebView.setWebChromeClient(new WebChromeClient(){
                        public void onProgressChanged(
                                WebView view, int newProgress){
                                // Update the progress bar
                                // with page loading progress
                                mProgressBar.setProgress(newProgress);

                                // Show the progress
                                mButton.setText(
                                        "URL Loaded " + newProgress + "%");

                                if(newProgress == 100){
                                        // Hide the progressbar
                                        mProgressBar.setVisibility(View.GONE);
                                        mButton.setText("Load URL");
                                }
                        }
                });


                // Enable the javascript
                mWebView.getSettings().setJavaScriptEnabled(true);

                // Render the web page
                mWebView.loadUrl(urlToRender);
        }



        // Method to show toast message
        private void showToast(String message){
                Toast.makeText(mContext,message,Toast.LENGTH_SHORT).show();
        }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="Load URL"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <WebView
        android:id="@+id/webView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/progressBar" />

</androidx.constraintlayout.widget.ConstraintLayout>