Reputation: 11
Following is the snip of relevant portion of resource file (activity_main.xml):
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_get"
android:onClick="getData" />
<ProgressBar
android:id="@+id/pbar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
The following is snip from MainActivity.java:
public class MainActivity extends Activity {
...
private ProgressBar spinner;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
spinner = (ProgressBar) findViewById(R.id.pbar);
spinner.setVisibility(View.INVISIBLE);
...
}
protected void getData(View view) {
...
spinner.setVisibility(View.VISIBLE);
// Do some task here
spinner.setVisibility(View.INVISIBLE);
...
}
The progress bar is not shown after clicking the button. However, as per some documentation that I read through, it is necessary to start the progress bar in a thread instead of the above way. How should a threaded implementation for progress bar be done for the above design
Upvotes: 1
Views: 1140
Reputation: 27
Instead of using the progress bar directly in widget you can try the following..
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
ProgressDialog ProgressDialog = new ProgressDialog(this);
ProgressDialog
.setMessage("Please wait...");
ProgressDialog.setIndeterminate(false);
ProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
ProgressDialog.show();
ProgressDialog.setCanceledOnTouchOutside(false);
ProgressDialog.setCancelable(false);
return ProgressDialog;
default:
return null;
}
}
/**
* Background Async Task
* */
class IsContinue extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(0);
}
/**
* Background task
*/
@Override
protected String doInBackground(String... params) {
// Do something here
}
/**
* After executing background task
*/
protected void onPostExecute(String Value) {
dismissDialog(0);
}
}
Upvotes: 1