Horizontal Progress Dialog with percentage

public class XYZ extends Activity {

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;

private Button startBtn;

private ProgressDialog mProgressDialog;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

startBtn = (Button) findViewById(R.id.startBtn);

startBtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

startDownload();

}

});

}

private void startDownload() {

String url = http://.jpg”;

new DownloadFileAsync().execute(url);

}

@Override

protected Dialog onCreateDialog(int id) {

switch (id) {

case DIALOG_DOWNLOAD_PROGRESS:

mProgressDialog = new ProgressDialog(this);

mProgressDialog.setMessage(“Downloading file..”);

mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

mProgressDialog.setCancelable(false);

mProgressDialog.show();

return mProgressDialog;

default:

returnnull;

}

}

class DownloadFileAsync extends AsyncTask<String, String, String> {

@Override

protected void onPreExecute() {

super.onPreExecute();

showDialog(DIALOG_DOWNLOAD_PROGRESS);

}

@Override

protected String doInBackground(String… aurl) {

int count;

try {

URL url = new URL(aurl[0]);

URLConnection conexion = url.openConnection();

conexion.connect();

int lenghtOfFile = conexion.getContentLength();

Log.d(“ANDRO_ASYNC”, “Lenght of file: “ + lenghtOfFile);

InputStream input = new BufferedInputStream(url.openStream());

OutputStream output = new FileOutputStream(

“/sdcard/some_photo_from_gdansk_poland.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) {

}

returnnull;

}

protected void onProgressUpdate(String… progress) {

Log.d(“ANDRO_ASYNC”, progress[0]);

mProgressDialog.setProgress(Integer.parseInt(progress[0]));

}

@Override

protected void onPostExecute(String unused) {

dismissDialog(DIALOG_DOWNLOAD_PROGRESS);

}

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s