How to use Progress bar ,Swipe refresh Layout and custom Web Page not available at the same activity in Android Studio - 2019

Awesome Tips
0
How to use Progress bar ,Swipe refresh Layout and custom Web Page not available at the same activity in Android Studio
In this post we are going to show to How to use Progress bar(with counting) ,Swipe refresh Layout and Custom Web Page not available error page at the same activity in Android Studio.If you want to Swipe refresh layout and custom error page only,watch below 2 videos.
If you want to add progress bar also please look at final code in this blog post.


Web Page not available error in Android WebView(Custom Error page)



Pull to refresh in Webview
 
activity_webview.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/liProgressContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <ProgressBar
                    android:id="@+id/progressBar"
                    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFF"
                    android:indeterminate="true" />

                <TextView
                    android:id="@+id/tvLoadingPercentage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFF" />

            </LinearLayout>

            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/liProgressContainer">

                <WebView
                    android:id="@+id/webView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </androidx.core.widget.NestedScrollView>
        </RelativeLayout>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111" />

</RelativeLayout>
webview.java

public class web_view extends AppCompatActivity {

    SwipeRefreshLayout swipe;
    ProgressBar progressBar;
    WebView webView;
    String url="http://blog.google.com";
    TextView textView;
    //to hide progressbar after loading part 1    
    LinearLayout liProgressContainer;
    private String currentUrl;
    private AdView mAdView;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);


        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");

        //banner ad view
        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        webView = findViewById(R.id.webView);
        progressBar = findViewById(R.id.progressBar);
        textView = findViewById(R.id.tvLoadingPercentage);
        //to hide progressbar after loading part 2        
        liProgressContainer = findViewById(R.id.liProgressContainer);
        swipe = findViewById(R.id.swipe);

        webView.setWebViewClient(new MyWebViewClient());

        webView.setWebChromeClient(new WebChromeClient() 
        {
            public void onProgressChanged(WebView view, int progress) 
            {
                progressBar.setProgress(progress);
                textView.setText(progress + " %");
            }
        });

        WebSettings browserSetting = webView.getSettings();
        browserSetting.setJavaScriptEnabled(true);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() 
        {
            @Override           
            public void onRefresh() 
            {
                LoadWeb();
            }
        });

        LoadWeb();
    }


    public void LoadWeb() 
    {
        webView.loadUrl(url);
        swipe.setRefreshing(true);
    }


    //back button function    
   @Override    
   public void onBackPressed() 
    {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    private class MyWebViewClient extends WebViewClient 
     {

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            webView.loadUrl("file:///android_asset/error.html");
        }

        @Override        
        public void onPageStarted(WebView view, String url, Bitmap favicon) 
        {
            liProgressContainer.setVisibility(View.VISIBLE);
            super.onPageStarted(view, url, favicon);
        }

        @Override       
         public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
            //progressBar.setVisibility(View.VISIBLE);            
            view.loadUrl(url);
            return true;
            //return super.shouldOverrideUrlLoading(view, url);        
        }

        @Override        
            public void onPageFinished(WebView view, String url) 
        {
            super.onPageFinished(view, url);
            liProgressContainer.setVisibility(View.GONE);
            swipe.setRefreshing(false);

        }
    }
}


watch below video to know about,
Android AdMob Tutorial 2019 - 01 - Integrate Google Mobile Ads SDK



Admob Official quick start guide here

In this blog post showed you to How to use Progress bar ,Swipe refresh Layout and custom Web Page not available at the same activity in Android Studio.Thank you.

Post a Comment

0 Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top