Search code examples
androiddialogthread-sleep

how to Show loading box while sending data?


Hi everyone i am facing problem in showing loading box on pressing send button using send_Schedule() function. I have the following code.

@Override
public void onClick(View arg0) {
pdialog.setCancelable(true);
pdialog.setMessage("Loading ....");
pdialog.show();
send_Schedule();

}

In send_Schedule() function i am putting delay of 3 secs like this in the following code. but dialog box always shows up after completion of loop.

send_Schedule(){
    for(int i=0;i<100;i++){
       Log.d("TAG",""+i)
       try {
     Thread.sleep(3000);
      Log.e("----------------", "-----------------");
     } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
   }
   pdialog.dismiss();
 }

I want to show dialog box while sending data...


Solution

  • Try this..

     private class YourTaskLoader extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progressDialog;
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(YourActivity.this);
            progressDialog.setCancelable(false);
            progressDialog.setMessage("Importing Messages...!");
            progressDialog.show();
        }
    
        @Override
        protected Void doInBackground(Void... params) {
            // Write you back ground logic here
            return null;
        }
    
        @Override
        protected void onPostExecute(Void result) {
            progressDialog.dismiss();
            super.onPostExecute(result);
        }
    }
    

    Invoke like in your Activity

    new YourTaskLoader().execute();
    

    Check this more info AsyncTask