Breaking News

How to create a Progress bar with web view in android



In Android Programming, When We create a Web Browser, then we have to implement a Web view in our XML file. and when we open a WebPage then we have to create a progress bar with it, to assure that link is clicked or not, or link will be opened or not.

link to the previous post

In this Post, we will create a horizontal Progress bar with relation to Web view.

For that, create a progress bar and toolbar in your XML file.
activitymain.xml
 <android.support.v7.widget.Toolbar  
   android:id="@+id/toolbar"  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   app:title=""  
   app:popupTheme="@style/ThemeOverlay.AppCompat.Light"  
   
   app:layout_constraintTop_toTopOf="@id/progressbar">  
   
     
 </android.support.v7.widget.Toolbar>  
   
   <ProgressBar  
     android:id="@+id/progressbar"  
     android:layout_width="match_parent"  
     android:layout_height="3dp"  
     android:indeterminate="true"  
     android:scaleY="5.0"  
     style="@style/Widget.AppCompat.ProgressBar.Horizontal"  
     app:layout_constraintBottom_toBottomOf="@id/toolbar"  
     app:layout_constraintBottom_toTopOf="@id/webview"/>  

Then create an instance and find the id in MainActivity
   
   ProgressBar progressbar;  
   Toolbar mToolbar;  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
   
     mToolbar = findViewById(R.id.toolbar);  
     progressbar = findViewById(R.id.progressbar);  

Then set visibility for your progress bar.
In onPageStarted setVisibility to Visible and in onPageFinished setVisibility to Gone
 @Override  
       public void onPageStarted(WebView view, String url, Bitmap favicon) {  
         progressbar.setVisibility(View.VISIBLE);  
           
         super.onPageStarted(view, url, favicon);  
       }  
   
       @Override  
       public void onPageFinished(WebView view, String url) {  
         progressbar.setVisibility(View.GONE);  
           
         super.onPageFinished(view, url);  
       }  
   
       @Override  
       public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {  
         return super.shouldOverrideUrlLoading(view, request);  
       }  
     });  

Now Your Progress bar will be visible when you click on a link or opens a WebPage.

Create a Full Webview

Rounded Corner in Edit Text in  android

No comments