Android ProgressBar Show Count Progress with Examples

Hi Everyone in this android development tutorial, I am sharing Android ProgressBar Show Count Progress with Examples. Android ProgressBar tutorial here you can learn how to show a number in progress bar.

By default the ProgressBar will be displayed as a spinning wheel, in case if we want to show it like a horizontal bar then we need to change the style property to horizontal like.

style=”?android: attr/progressBarStyleHorizontal”.

Android ProgressBar Show Count Progress in XML Layout File

In android, we can create ProgressBar in the XML layout file using <ProgressBar> Teg with different attributes.

<ProgressBar
    android:id="@+id/determinateBar"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="20dp"
    android:progressTint="@color/green"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:progress="0"/>

 

Attribute Description
android: id It is used to uniquely identify the control
android:minHeight It is used to set the height of the progress bar.
android:minWidth It is used to set the width of the progress bar.
android: max It is used to set the maximum value of the progress bar.
android: progress It is used to set the default progress value between 0 and max. It must be an integer value.

So Let’s Create a Design For Progress bar with Min and Max Value.

<ProgressBar
    android:id="@+id/determinateBar"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="20dp"
    android:progressTint="@color/green"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:progress="0"/>

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginLeft="10dp"
         android:layout_marginRight="10dp"
         android:orientation="horizontal">

         <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:padding="5dp"
             android:textColor="@color/black"
             android:textStyle="bold"
             android:text="0"/>

         <TextView
             android:id="@+id/MaxValue"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:padding="5dp"
             android:textColor="@color/black"
             android:gravity="right"
             android:textStyle="bold"
             android:text="0"/>
     </LinearLayout>

 Add these code in MainActivity.Java Class.

in your java file you and implement the progress bar and used a textView for show a min and max value on TextView.

 public class Step_Counter extends AppCompatActivity {
     ProgressBar determinateBar;TextView MaxValue;

    @SuppressLint("NewApi")
    @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_step__counter);

       MaxValue  = (TextView) findViewById(R.id.MaxValue);
       determinateBar=(ProgressBar)findViewById(R.id.determinateBar);
       determinateBar.setMax(1000);
       MaxValue.setText("1000");

       if (Integer.parseInt(HWSteps)>=1000){
          determinateBar.setMax(5000);
          MaxValue.setText("5000");
           }
       else if(Integer.parseInt(HWSteps)>=5000){
              determinateBar.setMax(10000);
              MaxValue.setText("10000");
      }else if (Integer.parseInt(HWSteps)>=10000){
              determinateBar.setMax(20000);
              MaxValue.setText("20000");
    }
     determinateBar.setProgress(Integer.parseInt(HWSteps));
    }
  }

 

 

You may also like...