How to Set a Progress Bar on Toolbar Actionbar in Android Java

Set a ProgressBar in Android WebView

A progress bar is useful to let your users know that the app is doing something in the background,loading a web page etc. a progress bar is a must have element in an android app where it is required.

Without a progress bar, your users might think that your app is froze,stuck or unresponsive. this tutorial will teach you how to add a rotating and horizontal progress bars in the toolbar of your android app.

Progress bars on tool bar in android

First, we need define a progress bar in the XML code

XML code to show a rotating progressbar in the toolbar

This is a rotating progressbar and you need a custom toolbar so use a theme such as Theme.AppCompat.NoActionBar in your app (Styles.xml or in manifest).

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar1"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_collapseMode="parallax"
            app:layout_scrollFlags="scroll|enterAlways">

            <ProgressBar
                android:id="@+id/toolbarprogress"
                android:layout_width="25dp"
                android:rotation="@integer/google_play_services_version"
                android:layout_height="25dp" />

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

 

Now go to the java activity where you want set the toolbar

Java code to hide and show rotating progressbar

//identify the Progress bar
ProgressBar progbar = findViewById(R.id.toolbarprogress);

//to show the progressbar
 progbar.setVisibility(View.VISIBLE);

// to hide the progressbar
progbar.setVisibility(View.GONE);

Result

 

The result 1 is the rotating progressbar, 2 is the horizontal proggressbar

 




Next, let’s learn how to set a horizontal progress bar, it is a different progress bar that is used to show the correct progress of doing something

 

XML code for Horizontal Progress bar

This progressbar is placed inside a frame layout for better fit below the toolbar

<android.support.design.widget.AppBarLayout
       android:layout_width="match_parent"
       android:layout_height="?android:attr/actionBarSize"
       app:layout_scrollFlags="scroll|enterAlways">

       <android.support.v7.widget.Toolbar
           android:id="@+id/toolbar1"
           android:layout_width="match_parent"
           android:layout_height="?android:attr/actionBarSize"
           android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
           app:layout_collapseMode="parallax"
           app:layout_scrollFlags="scroll|enterAlways">

       </android.support.v7.widget.Toolbar>

   </android.support.design.widget.AppBarLayout>





       <FrameLayout
           android:layout_marginTop="-4dp"
           android:id="@+id/framlytt"
           android:layout_width="match_parent"
           android:layout_height="9dp">

           <ProgressBar
               android:id="@+id/toolbarprogress"
               style="?android:attr/progressBarStyleHorizontal"
               android:layout_width="match_parent"
               android:layout_height="8dp"
               android:layout_marginTop="2dp"
               android:progress="30"
               android:theme="@style/Progressbar.White" />

       </FrameLayout>

 

Java code for Horizontal progressbar

The java code for the rotating progressbar is not useful for the horizontal progress bar because it needs adjust its horizontal length depending on the current progress state, so we need different code, here is an example, i have set the java code to show it in a webview, you need adapt this code for your app

 

ProgressBar progbar = findViewById(R.id.toolbarprogress);

webview.setWebChromeClient(new WebChromeClient() {

          public void onProgressChanged(WebView view, int newProgress) {
              super.onProgressChanged(view, newProgress);

              progbar.setProgress(newProgress);



              //loadingTitle.setProgres s(newProgress);
              // hide the progress bar if the loading is complete
              if (newProgress == 100) {


                  loadingProgressBar.setVisibility(View.INVISIBLE);

              } else {

                  loadingProgressBar.setVisibility(View.VISIBLE);


              }
          }




Related Posts

Add Admob ads in recyclerview in android

How to add admob native ads to recyclerview in android java Adding admob native ads into android recyclerview isn’t easy like other admob ads. You will need…

Download WebView Flutter Source Code for Android and iOS

Easily create android and iOS WebView app for your websites, the much-awaited flutter WebView source code is here. Are you looking for a flutter WebView source code…

Fix uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:_flutter_android]

This error can be fixed by modifying build.gradle file in the flutter In android studio: go to android>app>build.gradle and modify it as follows:

Get Your Own Android Mobile App for Your Website Today

Are you looking to expand your online presence and reach more customers? In today’s digital age, having a mobile app is an essential tool to connect with…

Why making an app for your website can help your business to grow

In today’s digital age, it’s more important than ever to have a strong online presence. Whether you’re running a business, a blog, or a personal website, having…

Fix android.content.Intent.migrateExtraStreamToClipData(android.content.Context)’ on a null object reference

Here is how to Fix Android Studio Error Attempt to invoke virtual method ‘boolean android.content.Intent.migrateExtraStreamToClipData(android.content.Context)’ on a null object reference This error is caused by a number…

Leave a Reply

Your email address will not be published. Required fields are marked *