Luke Batley
Luke Batley

Reputation: 2402

Android add a loading view to an AysncTask

Hi there i have an Asynctask that wroks only problem is the screen is blank until everything has loaded so i have created a loadingcell view but i'm wanting to know how to have it show the loading view until everything is loaded.

here what i have tried but it doesn't work

public class PostTask extends AsyncTask<Void, String, Boolean> {



        @Override
        protected Boolean doInBackground(Void... params) {
            boolean result = false;


            loadFixtures();
            publishProgress("progress");
            loadResultsFeed();
            publishProgress("progress");
            loadNewsFeed();
            publishProgress("progress");
            return result;
        }


        protected void onProgressUpdate(String... progress) {
            StringBuilder str = new StringBuilder();
                for (int i = 1; i < progress.length; i++) {
                    str.append(progress[i] + " ");

                  loadingView = LayoutInflater.from(getBaseContext()).inflate(R.layout.loadingcell,
                         null);

                }
        }

            @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            Log.v("BGThread", "begin fillin data");
             FillData();
        }
    }

Upvotes: 0

Views: 250

Answers (3)

Umesh
Umesh

Reputation: 1609

Try this.

private class BackgroundTask1 extends
        AsyncTask<String, Void, String> {

private ProgressDialog dialog;

    protected void onPreExecute() {
        /** Initialise progress dialog and show*/
        dialog = new ProgressDialog(currentActivity.this);
        dialog.setMessage("Loading.....");
        dialog.setCancelable(false);
        dialog.show();
    }


    protected String doInBackground(final String... args) {
        // do your stuff
               return anyString // as an example

    }


    protected void onPostExecute(String temp) {
        Log.v("String:::::",temp);
        dialog.dismiss();  // dismiss progress dialog
    }
}

Upvotes: 0

Nirali
Nirali

Reputation: 13805

I have done this way. You can refer

 private class DownloadingProgressTask extends
        AsyncTask<String, Void, Boolean> {

    private ProgressDialog dialog = new ProgressDialog(ShowDescription.this);


    protected void onPreExecute() {
        this.dialog.setMessage("Please wait");
        this.dialog.show();         

    }

    protected Boolean doInBackground(final String... args) {
        try {
            downloadFile(b.getString("URL"));
            return true;
        } catch (Exception e) {
            Log.e("tag", "error", e);
            return false;
        }
    }

    @Override
    protected void onPostExecute(final Boolean success) {

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        if (success) {
            Toast.makeText(ShowDescription.this,
                    "File successfully downloaded", Toast.LENGTH_LONG)
                    .show();
            imgDownload.setVisibility(8);
        } else {
            Toast.makeText(ShowDescription.this, "Error", Toast.LENGTH_LONG)
                    .show();
        }
    }

}

Upvotes: 0

MAC
MAC

Reputation: 15847

protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        Log.v("BGThread", "begin fillin data");
         FillData();
     // hide your view here                                <---------
     // for ex:    
      progressbar.setVisibility(View.GONE);                <---------

    }

Upvotes: 1

Related Questions