How To Show Progress Bar Status By Percentage
I am using progress dialog for android 2.3.3. Progress dialog's status is showing with the format as '60% 60/100' but I want to display only percentage not '60/100'. How can I do i
Solution 1:
According to documentation, you should call setProgressNumberFormat (null)
on your ProgressDialog
instance to get this behavior
Solution 2:
Hey Here Downloading a file from a server, saving a file to the sdcard and showing a progress bar.
publicclassXYZextendsActivity {
publicstaticfinalintDIALOG_DOWNLOAD_PROGRESS=0;
private Button startBtn;
private ProgressDialog mProgressDialog;
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startBtn = (Button)findViewById(R.id.startBtn);
startBtn.setOnClickListener(newOnClickListener(){
publicvoidonClick(View v) {
startDownload();
}
});
}
privatevoidstartDownload() {
Stringurl="http://.jpg";
newDownloadFileAsync().execute(url);
}
@Overrideprotected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = newProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
returnnull;
}
}
classDownloadFileAsyncextendsAsyncTask<String, String, String> {
@OverrideprotectedvoidonPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Overrideprotected String doInBackground(String... aurl) {
int count;
try {
URLurl=newURL(aurl[0]);
URLConnectionconexion= url.openConnection();
conexion.connect();
intlenghtOfFile= conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStreaminput=newBufferedInputStream(url.openStream());
OutputStreamoutput=newFileOutputStream("/sdcard/some_photo_from_gdansk_poland.jpg");
byte data[] = newbyte[1024];
longtotal=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;
}
protectedvoidonProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@OverrideprotectedvoidonPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
}
like this you get ..
this is example image i found on net.
and this is my xml file:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello" /><Buttonandroid:text="Start long running task.."android:id="@+id/startBtn"android:layout_width="fill_parent"android:layout_height="wrap_content"></Button></LinearLayout>
Solution 3:
Please visit this link.
Also It's explained in full in ApiDemos inside the SDK. The example that you want is named: ProgressBar3.java and can be found in \ApiDemos\src\com\example\android\apis\view\
Also, if you want to remove the borders of the dialog so that only the progress bar appears you can define the dialog itself as a transparent view/overlay (it's explained in the examples as well).
Post a Comment for "How To Show Progress Bar Status By Percentage"