I have a task to add a ProgressBar
with percentage values on it to show a value when the user wants to predict something. I\'m not using ProgressDialog
Following is the code to create a progress dialog with percentage like in the picture.
int status = 0;
Handler handler = new Handler();
public void showDialog(Activity activity, String msg) {
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
final ProgressBar text = (ProgressBar) dialog.findViewById(R.id.progress_horizontal);
final TextView text2 = dialog.findViewById(R.id.value123);
new Thread(new Runnable() {
@Override
public void run() {
while (status < 100) {
status += 1;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
text.setProgress(status);
text2.setText(String.valueOf(status));
if (status == 100) {
dialog.dismiss();
}
}
});
}
}
}).start();
dialog.show();
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
}
Layout File : dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp" />
<RelativeLayout
android:layout_width="wrap_content"
android:paddingLeft="20dp"
android:layout_margin="16dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/value123"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/value123"
android:text="%" />
</RelativeLayout>
Result :
With Asyntask :
int status = 0;
Handler handler = new Handler();
Dialog dialog;
ProgressBar text;
TextView text2;
public void showDialog(Activity activity, String msg) {
dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
text = (ProgressBar) dialog.findViewById(R.id.progress_horizontal);
text2 = dialog.findViewById(R.id.value123);
dialog.show();
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
new Thread(new Runnable() {
@Override
public void run() {
while (status < 100) {
status += 1;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
text.setProgress(status);
text2.setText(String.valueOf(status));
if (status == 100) {
status = 0;
}
}
});
}
}
}).start();
}
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
// it will fill the progress bar to 100
// and will refill again and again
/*
*
* Make your api call here
* MAke request to API and return data when response comes
*
*
* */
/*
*
* I have just add sleep to thead for example purposes
*
* */
try {
Thread.sleep(30 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return resp;
}
@Override
protected void onPostExecute(String result) {
/*
*
*
* Do your processing there on the api result
*
*
* */
dialog.dismiss();
}
@Override
protected void onPreExecute() {
showDialog(MainActivity.this,"");
}
@Override
protected void onProgressUpdate(String... text) {
}
}
For start the Asyntask :
new AsyncTaskRunner().execute("fs");
I am not sure if this is the best approach