Progress bars are used to show progress of a task. In this example, we will show you a simple application to illustrate the use of progress bar in Android development. It mainly use the class android.widget.ProgressBar.
In this example, there is a secondary progress displayable on a progress bar which is useful for displaying intermediate progress, such as the buffer level during a streaming playback progress bar.
activity_main.xml This is the XML file for the layout of the application.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ProgressBar android:id="@+id/firstBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone" /> <ProgressBar android:id="@+id/secondBar" style="?android:attr/progressBarStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" /> <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="begin"/> </LinearLayout>
Activity01.java This is the main Activity class.
package com.easyinfogeek.progress; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; public class Activity01 extends Activity { private ProgressBar firstBar = null; private ProgressBar secondBar = null; private Button myButton; private int i = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); firstBar = (ProgressBar)findViewById(R.id.firstBar); secondBar = (ProgressBar)findViewById(R.id.secondBar); myButton = (Button)findViewById(R.id.myButton); myButton.setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { if (i == 0 || i == 10) { //make the progress bar visible firstBar.setVisibility(View.VISIBLE); firstBar.setMax(150); secondBar.setVisibility(View.VISIBLE); }else if ( i< firstBar.getMax() ) { //Set first progress bar value firstBar.setProgress(i); //Set the second progress bar value firstBar.setSecondaryProgress(i + 10); }else { firstBar.setProgress(0); firstBar.setSecondaryProgress(0); i = 0; firstBar.setVisibility(View.GONE); secondBar.setVisibility(View.GONE); } i = i + 10; } } }