anjaly
anjaly

Reputation: 518

Showing activity indicator in android

I am new to android.I need to show an activity indicator while synchronizing.On clicking a button,i am redirecting to a new activity.In this new activity i am synchronizing.I need to show an activity indicator on button click till it is synchronized.

Upvotes: 1

Views: 9075

Answers (1)

Arun C
Arun C

Reputation: 9035

Synchronizing is effectively a network task And you have to do this on Background ( Async Task) So you can call an AsyncTask in your new Activity

private class SyncOperation extends AsyncTask<String, Void, String> {

      ProgressDialog progressDialog;

      @Override
      protected String doInBackground(String... params) {

          // Synchronize code here

             return null;
      }      

      @Override
      protected void onPostExecute(String result) {   
                    if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }            
      }

      @Override
      protected void onPreExecute() {
                     if (progressDialog == null) {
                    progressDialog = new ProgressDialog(MainActivity.this);
                    progressDialog.setMessage("Synchronizing, please wait...");
                    progressDialog.show();
                    progressDialog.setCanceledOnTouchOutside(false);
                    progressDialog.setCancelable(false);
                }   
      }


}

Now in OnCreate() of new Activity

SyncOperation syncTask=new SyncOperation();
syncTask.execute();

It will show a loader like

enter image description here

Upvotes: 7

Related Questions