Examples of ProgressBar and AsyncTask

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidprogressbar.MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"
/>

<Button
android:id="@+id/startprogress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start ProgressBar"/>

<ProgressBar
android:id="@+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />

<ProgressBar
android:id="@+id/determinateBar1"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="0"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/determinateBar2"
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/determinateBar3"
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/determinateBar4"
style="@android:style/Widget.ProgressBar.Large.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/determinateBar5"
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ProgressBar
android:id="@+id/determinateBar6"
style="@android:style/Widget.ProgressBar.Small.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>

package com.blogspot.android_er.androidprogressbar;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

Button btnStartProgress;
ProgressBar indeterminateBar;
ProgressBar determinateBar1, determinateBar2, determinateBar3,
determinateBar4, determinateBar5, determinateBar6;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnStartProgress = (Button)findViewById(R.id.startprogress);
indeterminateBar = (ProgressBar)findViewById(R.id.indeterminateBar);
determinateBar1 = (ProgressBar)findViewById(R.id.determinateBar1);
determinateBar2 = (ProgressBar)findViewById(R.id.determinateBar2);
determinateBar3 = (ProgressBar)findViewById(R.id.determinateBar3);
determinateBar4 = (ProgressBar)findViewById(R.id.determinateBar4);
determinateBar5 = (ProgressBar)findViewById(R.id.determinateBar5);
determinateBar6 = (ProgressBar)findViewById(R.id.determinateBar6);

btnStartProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnStartProgress.setEnabled(false);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
}
});
}

public class MyAsyncTask extends AsyncTask<Void, Integer, Void> {

@Override
protected void onPreExecute() {
indeterminateBar.setVisibility(View.VISIBLE);
determinateBar1.setVisibility(View.VISIBLE);
determinateBar1.setProgress(0);
determinateBar2.setVisibility(View.VISIBLE);
determinateBar3.setVisibility(View.VISIBLE);
determinateBar4.setVisibility(View.VISIBLE);
determinateBar5.setVisibility(View.VISIBLE);
determinateBar6.setVisibility(View.VISIBLE);
}

@Override
protected Void doInBackground(Void... voids) {
for(int i=0; i<100; i++){
publishProgress(i);
SystemClock.sleep(100);
}
return null;
}

@Override
protected void onProgressUpdate(Integer... values) {
determinateBar1.setProgress(values[0]);
}

@Override
protected void onPostExecute(Void aVoid) {
indeterminateBar.setVisibility(View.GONE);
determinateBar1.setVisibility(View.GONE);
determinateBar2.setVisibility(View.GONE);
determinateBar3.setVisibility(View.GONE);
determinateBar4.setVisibility(View.GONE);
determinateBar5.setVisibility(View.GONE);
determinateBar6.setVisibility(View.GONE);
btnStartProgress.setEnabled(true);
}
}
}

Next:
– Custom ProgressBar with progressDrawable

Android-er