Create ProgressBar |Progress Bar webview app source code

Create ProgressBar |Add ProgressBar webView app in Android studio|Android Studio 3.5|Tech Aside



Learn from youtube video









1. Add this code in  activity_main.xml(Copy and Paste)


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns: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"    
tools:context=".MainActivity"    
android:layout_gravity="center"    
android:gravity="center">
<ProgressBar
        
android:layout_width="match_parent"        
android:layout_height="8dp"        
android:id="@+id/progress"        
style="?android:attr/progressBarStyleHorizontal"        
android:layout_marginTop="-3dp"        
android:progress="20"        
android:visibility="gone">


 </ProgressBar>
   
 <WebView 
       
android:layout_width="match_parent"        
android:layout_height="match_parent"        
android:id="@+id/WebView">

</WebView>
    
</RelativeLayout>

2. Add this code MainActivity.java(Copy and Paste)











package com.webview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    private WebView WebView;
    ProgressBar progressBar;
    ProgressDialog progressDialog;

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

        progressBar=(ProgressBar)findViewById(R.id.progress) ;
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading wait....");


        WebView = (WebView) findViewById(R.id.WebView);

        WebView.getSettings().setJavaScriptEnabled(true);
        WebView.setWebViewClient(new WebViewClient());
        WebView.loadUrl("https://www.somoynews.tv/");


        WebView.setWebChromeClient(new WebChromeClient(){
            @Override           
      public void onProgressChanged(android.webkit.WebView view, int newProgress) {
                progressBar.setVisibility(View.VISIBLE);
                progressBar.setProgress(newProgress);
                setTitle("Loading....");
                progressDialog.show();
                if (newProgress == 100){
                    progressBar.setVisibility(View.GONE);
                    setTitle(view.getTitle());
                    progressDialog.dismiss();
                }
                super.onProgressChanged(view, newProgress);
            }
        });
    }
    //This method require to use back button if want to go previous web page
    @Override    public void onBackPressed() {

        if (WebView.canGoBack()) {
            WebView.goBack();

        } else {
            super.onBackPressed();
        }
    }
}


3.Add this code meansfet.xml



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








Download full webview app source code.





Download

No comments:

Post a Comment

Pages