Android – Showing activity indicator in android

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.

Best Solution

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