Progress Bar and Progress Dialog are useful to tell user that the task is takes longer time to finish. A Progress Dialog showing a progress indicator and an optional text message or view. Only a text message or a view can be used at the same time.
In this tutorial, we show you how to display a custom progress bar and use progress dialog in Asynctask to tell user that the file download task is running.
This project is developed in Eclipse 4.2.0.
1. Make main layout with some components: one button to show custom progress bars, one textview to show some infos, one button to start download image file from internet Url.
To customize progress bars on xml file above and a progress style of progress dialog when downloading file, we will edit style property of each progress bar.
For progress bar with horizoltal style:
Define CustomProgressBar style in res/values/styles.xml:
And in file res/drawable/custom_progress_bar_horizontal.xml
For progress bar with spinner style:
Content of res/drawable/my_progress_indeterminate.xml:
2.1. Starting a new asynctask to show custom two progress bars working when click on button. One horizontal style and one spinner style.
Create asyncTask class: ShowCustomProgressBarAsyncTask
2.2. When User clicks on the download file button, processing download and show custom progress dialog by starting a new asynctask.
In doInBackground method we process for download data from Url, then write to file located in SDCard.
After downloading image from the web then reading the downloaded image from the sdcard and displaying in a imageview, do this in onPostExecute method. All codes to work below:
Note: add some permission in androidmanifest.xml file to work with Internet and External Storage SDCard:
3.1. Run application
3.1. Click on first button
3.2. Click on Download file button
You can download all source codes of this tutorial from here.