Android – ProgressBar

Progress bars are used to show the progress of a task. Progress bar in Android is of two types, indeterminate, and determinate.
Indeterminate – When you don’t know the duration of progress, use this type of progress bar. The default type of ProgressBar is Indeterminate and it has cyclic animation.

<ProgressBar
     android:id=”@+id/progressbar”
     android:layout_width=”wrap_content”
     android:layout_height=”wrap_content”
     />
Determinate – This ProgressBar is used when you want to show percentage of progress in any task. You can set the percent of progress.

<ProgressBar
android:id=”@+id/ProgressBar” style=”@android:style/Widget.ProgressBar.Horizontal” android:layout_width=”wrap_content” android:layout_height=”wrap_content”
android:progress=”25″/>
This is a horizontal type of progress bar.

Some of the important attributes of ProgressBar are,
android:setProgress(int value) – With this attribute, progress value can be set.
android: max – This attribute sets the maximum value for ProgressBar
android: min – This determines the minimum value of ProgressBar
android: indeterminate – this is to determine if time is determinate or indeterminate
android: style – Default ProgressBar Style is circular, but different styles can be set with style attribute.
Some of the styles are,
Widget.ProgressBar.Horizontal
Widget.ProgressBar.Small
Widget.ProgressBar.Large
Widget.ProgressBar.Inverse
Widget.ProgressBar.Small.Inverse
Widget.ProgressBar.Large.Inverse

Android ProgressBar Example
<?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”
android:orientation=”vertical”
android:background=”#312F32″
>
<LinearLayout
android:layout_width=”match_parent”
android:layout_gravity=”center_horizontal”
android:gravity=”center”
android:layout_below=”@+id/purplechai”
android:layout_height=”wrap_content”
>
<ImageView
android:layout_width=”100dp”
android:layout_height=”100dp”
android:layout_gravity=”center_horizontal”
android:background=”@drawable/purplechai_logo”
/>
</LinearLayout>

<ProgressBar
android:id=”@+id/progressBar”
android:layout_centerInParent=”true”
style=”@android:style/Widget.ProgressBar.Horizontal”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentTop=”true”
android:layout_marginLeft=”23dp”
android:layout_marginTop=”20dp”
android:indeterminate=”false”
android:max=”100″
android:minHeight=”50dp”
android:minWidth=”200dp”
android:progress=”1″
/>
</RelativeLayout>

ProgressBarActivity
public class ProgressBarActivity extends AppCompatActivity {
private ProgressBar progressBar;
private int progressValue = 0;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
new Thread(new Runnable() {
public void run() {
while (progressValue < 100) {
progressValue += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressValue);
// set the value of progress
}
});
try {
// Sleep for 500 milliseconds.
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
In the above example, ProgressBar updates every 500 milliseconds.
This is a basic ProgressBar example we created.
For our expertise and help in app development services, please get in touch with http://purplechai.com/






	

Leave a Reply

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