0

I am new in android. I am trying to display ProgressDialog when click on button .

This is my code:

// set listener
btn_Login.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    //progress.show();

    MyAsynch    asynch = new MyAsynch();
    asynch.execute();
}

In this code progress dialog too much late appear when i am comment on Asynctask object then progress dialog appear normally.

I am puting my progress dialog in AsynchTask method

onPreExecute() but same out put dialog display late .

How to solve my problem..??

I am also read stack answers following link but not solve my problem .

async task progress dialog show too late

ProgressDialog appears too late and dissapears too fast

here is my Asynctask code

        private class MyAsynch extends AsyncTask<String, Void, String> {
        ProgressDialog progress;
        String login_stat;
        String stat;

        @Override
        protected void onPreExecute() {
            progress = new ProgressDialog(this);
            progress.setTitle(" User Login ");
            progress.setMessage("Please Wait!!");
            progress.setCancelable(false);
            progress.setIndeterminate(true);
            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progress.show();
        }
        @Override
        protected String doInBackground(String... urls) {

            try {

                login_stat = s_ApiHandling.doLogin(m_Et_Username.getText()
                        .toString().trim(), m_Et_Password.getText()
                        .toString().trim());


            } catch (Exception e) {
                System.out.println("internet connection loss ");
                stat = "ERORR";
                e.printStackTrace();
            }

            return stat;
        }


   @Override
 protected void onPostExecute(String status) {

                progress.dismiss();




            }

    }
Community
  • 1
  • 1
Jatinkumar Patel
  • 1,094
  • 2
  • 17
  • 34

3 Answers3

0

You are probably doing too much in onPreExecute

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
0

Remove progress.cancel() from your doInBackground method and put it in to a onPostExecute method in your AsyncTask (like the second link you posted)

You shouldn't have anything talking to the UI in a background thread - that should all be done in pre/post execution.

Ben Pearson
  • 7,532
  • 4
  • 30
  • 50
0

you code should look like this:

AsyncTask<String, Void, String>()
{
    private ProgressDialog progressDialog = ProgressDialog.show(this, "", "Loading...");

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

    @Override
    protected String[] doInBackground(String... params)
    {
        //ALL CODE GOES HERE.
    }
}

When you call the asynctask you must not use the get() method or the progress dialog won't work correctly.

string.Empty
  • 10,393
  • 4
  • 39
  • 67