Progress Bar In Android:
Has name implies Progress Bar means to progress the work done.In General if you are downloading the .jpge or .png or any video file file from the Network there you want to know the work progress like was its work is getting made.
This work can be display in terms of Bar, is Called Progress Bar, once the Progress starts our application value will get changed.
Progress Bar will show like 10,20,30,40,50,60,70,80,90,100 the default value is 100
its can not be 10,20,......100 it can be 10, 15, 21, 30, 44, 57,....81,97,100 based on the programmer choice that we will see in details with example...
Custom Progress Bar Example in Android:
File >> New >> Android Application
Enter Project name: CustomProgressBarAndDownloadImageExample
Package: com.example.customprogressbardownloadimageexample
Keep other default selections, click Next until you reach Finish

Copy and Past the following code in the MainActivity.java
package com.example.customprogressbardownloadimageexample;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ProgressbarActivity extends Activity {
int nProgress;
ProgressBar progressBar;
ProgressBar progressBarone;
TextView textview;
Button btn_Show, btn_Download;
private ProgressDialog progressDialog;
ImageView iv_Image;
public static final int CUSTOM_PROGRESS_DIALOG = 0;
private static String photoURL = "http://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progressbar);
textview = (TextView) findViewById(R.id.textView);
btn_Show = (Button) findViewById(R.id.button1);
btn_Download = (Button) findViewById(R.id.button2);
iv_Image = (ImageView) findViewById(R.id.my_image);
progressBarone = (ProgressBar) findViewById(R.id.progressBar2);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
progressBarone.setVisibility(View.INVISIBLE);
progressBar.setProgress(0);
progressBar.setSecondaryProgress(0);
}
public void button_click(View view) {
switch (view.getId()) {
case R.id.button1:
new ShowCustomProgressBarAsyncTask().execute();
progressBarone.setVisibility(View.VISIBLE);
break;
case R.id.button2:
new DownloadFileFromURL().execute(photoURL);
break;
}
}
public class ShowCustomProgressBarAsyncTask extends
AsyncTask<Void, Integer, Void> {
int nProgress;
@Override
protected void onPostExecute(Void result) {
textview.setText("Finish work with custom ProgressBar");
btn_Show.setEnabled(true);
progressBarone.setVisibility(View.INVISIBLE);
}
@Override
protected void onPreExecute() {
btn_Show.setEnabled(false);
textview.setText("Start work with custom ProgressBar");
nProgress = 0;
progressBar.setSecondaryProgress(0);
}
@Override
protected Void doInBackground(Void... params) {
while (nProgress < 100) {
nProgress++;
publishProgress(nProgress);
SystemClock.sleep(100);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
progressBar.setProgress(values[0]);
progressBar.setSecondaryProgress(values[0] + 5);
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case CUSTOM_PROGRESS_DIALOG:
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Downloading file. Please wait...");
progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgressDrawable(getResources().getDrawable(
R.drawable.custom_progress_bar_horizontal));
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
return progressDialog;
default:
return null;
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(CUSTOM_PROGRESS_DIALOG);
}
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(),
8192);
OutputStream output = new FileOutputStream(
"/sdcard/photo.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... value) {
progressDialog.setProgress(Integer.parseInt(value[0]));
progressDialog.setSecondaryProgress(Integer.parseInt(value[0]) + 5);
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String file_url) {
dismissDialog(CUSTOM_PROGRESS_DIALOG);
String imagePath = Environment.getExternalStorageDirectory()
.toString() + "/photo.jpg";
iv_Image.setImageDrawable(Drawable.createFromPath(imagePath));
}
}
}
Copy and Past the following code in the layout file called activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_marginBottom="2dp"
android:background="@android:color/white"
android:onClick="button_click"
android:text="Start Progress"
android:textColor="@android:color/black" />
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/CustomProgressBar"
android:layout_margin="5dip" />
<ProgressBar
android:id="@+id/progressBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="@drawable/my_progress_indeterminate"
android:layout_margin="5dip"/>
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:layout_margin="5dip"
android:textColor="#48D1CC"
android:textSize="18dp"/>
<Button
android:id="@+id/button2"
android:layout_width="247dp"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_marginBottom="20dp"
android:background="#FFFFFF"
android:onClick="button_click"
android:text="Download Started"
android:textColor="@android:color/black" />
<!-- Image view to show image after downloading -->
<ImageView android:id="@+id/my_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Copy and Past the following code in the Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.customprogressbardownloadimageexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ProgressbarActivity"
android:label="@string/title_activity_progressbar"
android:theme="@android:style/Theme.Holo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Note: past one images in the drawable folder of your choice i copied images named
Run Your Project as the below by clicking on shift+10 or like the below-->Android Application and observe the output
Has name implies Progress Bar means to progress the work done.In General if you are downloading the .jpge or .png or any video file file from the Network there you want to know the work progress like was its work is getting made.
This work can be display in terms of Bar, is Called Progress Bar, once the Progress starts our application value will get changed.
Progress Bar will show like 10,20,30,40,50,60,70,80,90,100 the default value is 100
its can not be 10,20,......100 it can be 10, 15, 21, 30, 44, 57,....81,97,100 based on the programmer choice that we will see in details with example...
Custom Progress Bar Example in Android:
File >> New >> Android Application
Enter Project name: CustomProgressBarAndDownloadImageExample
Package: com.example.customprogressbardownloadimageexample
Keep other default selections, click Next until you reach Finish

Copy and Past the following code in the MainActivity.java
package com.example.customprogressbardownloadimageexample;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ProgressbarActivity extends Activity {
int nProgress;
ProgressBar progressBar;
ProgressBar progressBarone;
TextView textview;
Button btn_Show, btn_Download;
private ProgressDialog progressDialog;
ImageView iv_Image;
public static final int CUSTOM_PROGRESS_DIALOG = 0;
private static String photoURL = "http://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progressbar);
textview = (TextView) findViewById(R.id.textView);
btn_Show = (Button) findViewById(R.id.button1);
btn_Download = (Button) findViewById(R.id.button2);
iv_Image = (ImageView) findViewById(R.id.my_image);
progressBarone = (ProgressBar) findViewById(R.id.progressBar2);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
progressBarone.setVisibility(View.INVISIBLE);
progressBar.setProgress(0);
progressBar.setSecondaryProgress(0);
}
public void button_click(View view) {
switch (view.getId()) {
case R.id.button1:
new ShowCustomProgressBarAsyncTask().execute();
progressBarone.setVisibility(View.VISIBLE);
break;
case R.id.button2:
new DownloadFileFromURL().execute(photoURL);
break;
}
}
public class ShowCustomProgressBarAsyncTask extends
AsyncTask<Void, Integer, Void> {
int nProgress;
@Override
protected void onPostExecute(Void result) {
textview.setText("Finish work with custom ProgressBar");
btn_Show.setEnabled(true);
progressBarone.setVisibility(View.INVISIBLE);
}
@Override
protected void onPreExecute() {
btn_Show.setEnabled(false);
textview.setText("Start work with custom ProgressBar");
nProgress = 0;
progressBar.setSecondaryProgress(0);
}
@Override
protected Void doInBackground(Void... params) {
while (nProgress < 100) {
nProgress++;
publishProgress(nProgress);
SystemClock.sleep(100);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
progressBar.setProgress(values[0]);
progressBar.setSecondaryProgress(values[0] + 5);
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case CUSTOM_PROGRESS_DIALOG:
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Downloading file. Please wait...");
progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgressDrawable(getResources().getDrawable(
R.drawable.custom_progress_bar_horizontal));
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
return progressDialog;
default:
return null;
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(CUSTOM_PROGRESS_DIALOG);
}
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(),
8192);
OutputStream output = new FileOutputStream(
"/sdcard/photo.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... value) {
progressDialog.setProgress(Integer.parseInt(value[0]));
progressDialog.setSecondaryProgress(Integer.parseInt(value[0]) + 5);
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String file_url) {
dismissDialog(CUSTOM_PROGRESS_DIALOG);
String imagePath = Environment.getExternalStorageDirectory()
.toString() + "/photo.jpg";
iv_Image.setImageDrawable(Drawable.createFromPath(imagePath));
}
}
}
Copy and Past the following code in the layout file called activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_marginBottom="2dp"
android:background="@android:color/white"
android:onClick="button_click"
android:text="Start Progress"
android:textColor="@android:color/black" />
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/CustomProgressBar"
android:layout_margin="5dip" />
<ProgressBar
android:id="@+id/progressBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="@drawable/my_progress_indeterminate"
android:layout_margin="5dip"/>
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:layout_margin="5dip"
android:textColor="#48D1CC"
android:textSize="18dp"/>
<Button
android:id="@+id/button2"
android:layout_width="247dp"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_marginBottom="20dp"
android:background="#FFFFFF"
android:onClick="button_click"
android:text="Download Started"
android:textColor="@android:color/black" />
<!-- Image view to show image after downloading -->
<ImageView android:id="@+id/my_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Copy and Past the following code in the style.xml Located in values folder res>>values>>style.xml
<resources>
<style name="CustomProgressBar" parent="android:Widget.ProgressBar.Horizontal">
<item name="android:indeterminateOnly">false</item>
<item
name="android:progressDrawable">@drawable/custom_progress_bar_horizontal</item>
<item name="android:minHeight">10dip</item>
<item name="android:maxHeight">20dip</item>
</style>
<style name="AppTheme" parent="android:Theme.Light" />
</resources>
<style name="CustomProgressBar" parent="android:Widget.ProgressBar.Horizontal">
<item name="android:indeterminateOnly">false</item>
<item
name="android:progressDrawable">@drawable/custom_progress_bar_horizontal</item>
<item name="android:minHeight">10dip</item>
<item name="android:maxHeight">20dip</item>
</style>
<style name="AppTheme" parent="android:Theme.Light" />
</resources>
Create
custom_progressbar.xml
file in drawable folder of res and Copy and Past the following code
<?xml version="1.0" encoding="UTF-8"?>
<!-- @author : @alexduhem
blog.sakaroz.com
-->
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@android:id/background">
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="#ffffffff"
android:centerColor="#ffdddddd"
android:centerY="0.50"
android:endColor="#ffffffff"
android:angle="270" />
</shape>
</item>
<item
android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="#770e75af"
android:endColor="#771997e1"
android:angle="90" />
</shape>
</clip>
</item>
<item
android:id="@android:id/progress">
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="#ff0e75af"
android:endColor="#ff1997e1"
android:angle="90" />
</shape>
</clip>
</item>
</layer-list>
Create progress_indeterminate.xml xml file Copy and Past the following code
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/loading"
android:pivotX="50%"
android:pivotY="50%" />
Copy and Past the following code in the Manifest.xml
package="com.example.customprogressbardownloadimageexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ProgressbarActivity"
android:label="@string/title_activity_progressbar"
android:theme="@android:style/Theme.Holo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Note: past one images in the drawable folder of your choice i copied images named
Run Your Project as the below by clicking on shift+10 or like the below-->Android Application and observe the output
OutPut:
When you click on the Start Progress button as shown in the images custom progress bar will start and there also a text will display as start work with custom progressbar
Now if you click on the second button download started button a progress bar will display and image will downloaded and displayed down
Finally:
Please Send Your Comments To ambilpura.sunil@gmail.com
Stay Tune For Next Tutorial...Displaying All Messages From Mobile Inbox Example In Android:
No comments:
Post a Comment