0

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.

Community
  • 1
  • 1
reetu
  • 313
  • 3
  • 11

3 Answers3

1

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).

pathfinderelite
  • 3,047
  • 1
  • 27
  • 30
  • I tried to call the displayAddTableView(editIsEnabled) from doInBacground() and at the table.show which is inside displayAddTableView(editIsEnabled) moved to onPostExecute. Then my spinner appears on button click. But crashing giving me below exception-java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare(). – reetu Nov 04 '16 at 19:13
  • Anything in `displayAddTableView` which updates the UI would need to be put inside an call to `runOnUiThread()`. – pathfinderelite Nov 04 '16 at 19:19
  • @reetu The table view should have a `post` method as well, which basically does the same thing. – pathfinderelite Nov 04 '16 at 19:20
  • I have done all UI stuff in runOnUiThread()-which is inside displayAddTableView. Now no exception but I don't see the progress bar either. – reetu Nov 04 '16 at 20:31
0

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();
Soheil Tayyeb
  • 315
  • 4
  • 13
0

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;
reetu
  • 313
  • 3
  • 11