What is Android ProgressBar ?
Progress bars are used to show progress of a task. For example. When you are uploading or downloading something from the internet, it is better to show the progress of download/upload to the user.
ProgressBar in Android is a graphical view indicator which shows some progress. It shows a bar representing the completing of the task. Normally the ProgresBar do not display the amount of completion in numbers. If we want, we can display it in the TextView.
Difference between ProgressBar and ProgressDialog:
Things to know about ProgressBar:
Method | Description |
---|---|
getMax() | This method returns the maximum value of the progress. |
incrementProgressBy(int diff) | This method increments the progress bar by the difference of value passed as a parameter. |
setIndeterminate(boolean indeterminate) | This method sets the progress indicator as determinate or indeterminate. |
setMax(int max) | This method sets the maximum value of the progress dialog. |
setProgress(int value) | This method is used to update the progress dialog with some specific value. |
show(Context context, CharSequence title, CharSequence message) | This is a static method, used to display progress dialog. |
In this example I will show you how to implement ProgressDialog and different types of ProgressBar like vertical, horizontal and Circular.
Let’s get start by creating a project in Eclipse IDE.
1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java).
2. Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml. In this layout i had taken three progressbar for three different style progress and some buttons to start the progress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="center" android:padding="5dp" > <Button android:id="@+id/start_horizontal_progressbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start Horizontal ProgressBar" /> <ProgressBar android:id="@+id/horizontal_progressbar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/horizontalprogress_percentage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:gravity="right" /> <Button android:id="@+id/start_vertical_progressbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start Vertical ProgressBar" /> <ProgressBar android:id="@+id/vertical_progressbar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="100dp" android:layout_gravity="center_horizontal" android:progressDrawable="@drawable/vertical_progressbar" /> <TextView android:id="@+id/verticalprogress_percentage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <Button android:id="@+id/download_file" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Download File" /> <Button android:id="@+id/start_circular_progressbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start Circular ProgressBar" /> <ProgressBar android:id="@+id/circular_progressbar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:progressDrawable="@drawable/circular_progressbar" /> <TextView android:id="@+id/circularprogress_percentage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> </LinearLayout> </ScrollView> |
3. For creating vertical and circular progressbar we have to make it by custom because bydefault we are having only horizontal progressbar. So, for custom progressbar create a new directory under res directory naming drawable and under that directory create two new xml files for vertical and circular progressbar.
This is for vertical progressbar naming vertical_progressbar.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <!-- By default progressbar --> <item android:id="@android:id/background"> <shape> <corners android:radius="5dip" /> <solid android:color="#FFFFFF" /> <stroke android:width="1dp" android:color="#FF000000" /> </shape> </item> <!-- on Progress Update --> <item android:id="@android:id/progress"> <clip android:clipOrientation="vertical" android:gravity="bottom" > <shape> <corners android:radius="5dip" /> <!-- Color to be shown on progressbar update --> <solid android:color="#FF00" /> <stroke android:width="1dp" android:color="#FF000000" /> </shape> </clip> </item> </layer-list> |
Now, make xml file for circular progressbar naming circular_progressbar.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <!-- By default Progress --> <item android:id="@android:id/secondaryProgress"> <shape android:innerRadiusRatio="3" android:shape="ring" android:thicknessRatio="14.0" > <gradient android:centerColor="#FFFFFF" android:endColor="#FFFFFF" android:startColor="#FFFFFF" android:type="sweep" /> </shape> </item> <!-- on Progress Update --> <item android:id="@android:id/progress"> <rotate android:fromDegrees="270" android:pivotX="50%" android:pivotY="50%" android:toDegrees="270" > <shape android:innerRadiusRatio="3" android:shape="ring" android:thicknessRatio="14.0" > <rotate android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360" /> <!-- Color to be shown when progress bar is updating --> <gradient android:centerColor="#00FF00" android:endColor="#00FF00" android:startColor="#00FF00" android:type="sweep" /> </shape> </rotate> </item> </layer-list> |
4. Now, come to MainActivity.java and add the following code. In this class I will show how start the progress of all Progressbar and increment the progress and show it over the TextView and except ProgressBar a ProgressDialog is also shown.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
package com.android_progressbar_demo; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.os.SystemClock; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends ActionBarActivity implements OnClickListener { private static ProgressBar horizontal_progressbar, vertical_progressbar, circular_progressbar; private static Button horizontal_bar_button, vertical_bar_button, download_file, circular_progressbar_buttom; private static TextView horizontal_status, vertical_status, circular_progressbar_status; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); setListeners(); } // Initialize all widgets void init() { horizontal_progressbar = (ProgressBar) findViewById(R.id.horizontal_progressbar); horizontal_bar_button = (Button) findViewById(R.id.start_horizontal_progressbar); horizontal_status = (TextView) findViewById(R.id.horizontalprogress_percentage); vertical_progressbar = (ProgressBar) findViewById(R.id.vertical_progressbar); vertical_bar_button = (Button) findViewById(R.id.start_vertical_progressbar); vertical_status = (TextView) findViewById(R.id.verticalprogress_percentage); download_file = (Button) findViewById(R.id.download_file); circular_progressbar = (ProgressBar) findViewById(R.id.circular_progressbar); circular_progressbar_buttom = (Button) findViewById(R.id.start_circular_progressbar); circular_progressbar_status = (TextView) findViewById(R.id.circularprogress_percentage); } // Set Click Listeners to all buttons void setListeners() { horizontal_bar_button.setOnClickListener(this); vertical_bar_button.setOnClickListener(this); download_file.setOnClickListener(this); circular_progressbar_buttom.setOnClickListener(this); } // Execute Async Task method on button click @Override public void onClick(View v) { switch (v.getId()) { case R.id.start_horizontal_progressbar: new UpdateHorizontalProgressBar().execute(); break; case R.id.start_vertical_progressbar: new UpdateVerticalProgressBar().execute(); break; case R.id.download_file: new DownloadFile().execute(); break; case R.id.start_circular_progressbar: new UpdateCircularProgressBar().execute(); break; } } // Update Horizontal progress bar asynctask public class UpdateHorizontalProgressBar extends AsyncTask<Void, Integer, Void> { // Progress variable for progressbar progress int progress; @Override protected void onPreExecute() { // Set Progress 0 at starting progress = 0; // Setting text to status textView horizontal_status.setText(progress + " %"); } @Override protected void onProgressUpdate(Integer... values) { // Update the progressbar and textView Text on Progress Update horizontal_progressbar.setProgress(values[0]); horizontal_status.setText(values[0] + " %"); } @Override protected Void doInBackground(Void... arg0) { // In Background increment the progress one by one till 100 while (progress < 100) { progress++; // Publish the progress publishProgress(progress); // Time wait of 100ms SystemClock.sleep(100); } return null; } } // Update Vertical progress bar asynctask public class UpdateVerticalProgressBar extends AsyncTask<Void, Integer, Void> { int progress; @Override protected void onPreExecute() { progress = 0; vertical_status.setText(progress + " %"); } @Override protected void onProgressUpdate(Integer... values) { vertical_progressbar.setProgress(values[0]); vertical_status.setText(values[0] + " %"); } @Override protected Void doInBackground(Void... arg0) { while (progress < 100) { progress++; publishProgress(progress); SystemClock.sleep(100); } return null; } } // Download File asynctask public class DownloadFile extends AsyncTask<Void, Integer, Void> { int progress; // Progress Dialog to show progress on dialog ProgressDialog dialog = new ProgressDialog(MainActivity.this); @Override protected void onPreExecute() { // Setting title to dialog dialog.setTitle("File is downloading..Please Wait.."); // This will make the dialog uncancelabe on outside or back clicking dialog.setCancelable(false); // Maximum value dialog.setMax(100); // Starting Progress dialog.setProgress(0); // Progress Style (Horizontal or Circular) dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // Spinner Style i.e. Circular // dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // Setting intermediate i.e. nothing happen when any intermediate is // happens dialog.setIndeterminate(false); // Showing dialog dialog.show(); progress = 0; } @Override protected void onPostExecute(Void result) { // Dismiss the dialog when task is done // dialog.hide(); dialog.dismiss(); super.onPostExecute(result); } @Override protected void onProgressUpdate(Integer... values) { // Update Progress the dialog dialog.setProgress(values[0]); } @Override protected Void doInBackground(Void... arg0) { while (progress < 100) { progress++; publishProgress(progress); SystemClock.sleep(100); } return null; } } // Update Circular progress bar asynctask public class UpdateCircularProgressBar extends AsyncTask<Void, Integer, Void> { int progress; @Override protected void onPreExecute() { progress = 0; circular_progressbar_status.setText(progress + " %"); } @Override protected void onProgressUpdate(Integer... values) { circular_progressbar.setProgress(values[0]); circular_progressbar_status.setText(values[0] + " %"); } @Override protected Void doInBackground(Void... arg0) { while (progress < 100) { progress++; publishProgress(progress); SystemClock.sleep(100); } return null; } } } |
5. Now, run the app and you will get the output as shown in video.
Thanks.
Subscribe to us and get the latest news.