reetu
reetu

Reputation: 313

ProgressDialog on a fragment class not appearing immediately after button click

I have a Fragment class on a button click I should display a tableview with contents,which is taking a time span of 4 seconds or more depending on the contents to be displayed. So I thought to show a progressDialog after button click and dismiss after my method execution.

But the process is appearing after a delay,like after the tableview appears.So there is a delay in showing the progress bar. I have tried so many approaches still no luck.

Below is the code I am trying.

  @Override
    public void onClick(final View v) {

        switch (v.getId()) {
        case R.id.btn_spreadsheetAddTable: {
            //Some code
        }
            break;

        case R.id.btn_horizontalAddTable: {

            AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {

                    return null;
                }

                @Override
                protected void onPreExecute() {
                    mProgressDialog = new ProgressDialog(getActivity());
                    mProgressDialog.setTitle("Processing...");
                    mProgressDialog.setMessage("Please wait.");
                    mProgressDialog.setCancelable(false);
                    mProgressDialog.setIndeterminate(true);
                    mProgressDialog.show();
                   displayAddTableView(editIsEnabled);

                }

                @Override
                protected void onPostExecute(Void result) {
                    if (mProgressDialog!=null) {
                        mProgressDialog.dismiss();

                    }
                }
            };
            task.execute((Void[])null);



            editIsEnabled = false;

        }

private void displayAddTableView(boolean editEnabled) {
//A code to display dynamic table with dynamic data
}
}

I have been tried the following urls also:

android - Progressbar Late Loading when button click

Loading Dialog, Progress Dialog on Button Click

ProgressDialog doesn't appear immediately

Please help me on this.Thank you.

Upvotes: 0

Views: 1523

Answers (3)

reetu
reetu

Reputation: 313

I have fixed this issue by adding a toast and running the table design code in a UIThread, since it was not possible to show progress since both design of my custom table and progress needs to run in same thread.

@Override
public void onClick(final View v) {

    switch (v.getId()) {
    case R.id.btn_spreadSheetAddRow: {


        Toast.makeText(this,
                "Loading",
                Toast.LENGTH_SHORT).show();

        final Thread uiThread = new Thread(new Runnable() {

            @Override
            public void run() {

                uiHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {

                        //Call the heavy table design code;

                        Toast.makeText(getApplicationContext(),
                                "Done",
                                Toast.LENGTH_SHORT).show();

                    }
                }, 80);
            }
        });
        uiThread.start();
    }
        break;

Upvotes: 0

pathfinderelite
pathfinderelite

Reputation: 3147

You say it's taking 4 seconds to display. Sounds like whatever it is your doing should be done in a background thread, not the UI thread. Maybe you want to execute displayAddTableView(editIsEnabled); in doInBackground, and post any UI updates.

mProgressDialog.show() causes messages to be posted to the UI message queue, meaning if your UI thread is busy running displayAddTableView(editIsEnabled);, the progress spinner will not show (or will show for a split second after displayAddTableView(editIsEnabled); has finished running).

Upvotes: 1

Soheil Tayyeb
Soheil Tayyeb

Reputation: 315

May be your device or simulator is slow .You can init progress dialog first and then in on onPreExecute show this :

ProgressDialog mProgressDialog;
.
.
.     //in oncreate
       mProgressDialog = new ProgressDialog(getActivity());
        mProgressDialog.setTitle("Processing...");
        mProgressDialog.setMessage("Please wait.");
        mProgressDialog.setCancelable(false);
        mProgressDialog.setIndeterminate(true);


.
.
.
protected void onPreExecute() {
mProgressDialog.show();

Upvotes: 0

Related Questions