How to show ProgressDialog In Android Fragments?

3.9k Views Asked by At

I have some tabpages and fragments. And these fragments contain a GridView element. My program is going to webservice and reading JSON data, after getting images caching and populating gridviews custom grid.

Now, how can show ProgressDialog only not completed fragments? And how can I dismiss progressDialog GridView populating will complete?

My Fragment OnCreate Method;

   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_b, container, false);
        context = getActivity();
        final ProgressDialog dialog = ProgressDialog.show(context, "", "Please wait, Loading Page...", true);
        GridView gridView = (GridView) v.findViewById(R.id.gridview);
        if (isConnected()) {
            try {
                new ProgressTask(context).execute();
                gridView.setAdapter(new AdapterB(context, WallPaperList));
            } catch (NullPointerException e) {
                e.printStackTrace();
                return v;
            }
        } else {
            Toast.makeText(v.getContext(), "Please check your internet connection..!", Toast.LENGTH_LONG).show();
        }

        return v;
    }
3

There are 3 best solutions below

7
On

In AsyncTask, create progress dialog in onPreExecute() and dismiss that progress dialog in onPostExecute().

updated:

add this code in your viewpager activity.

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

      }

      @Override
      public void onPageSelected(int position) {
          //You can get Call back to show the progress dialog when swipping.
          boolean isLoading = true;//Check if the fragment is still loading or not at given position.
          if (isLoading) {
                 //Show progress dialog.
          }
      }

      @Override
      public void onPageScrollStateChanged(int state) {
      }
 });
2
On

its the short example for showing progress may this help you

 private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(DownloadImageActivity.this, "Wait", "Downloading...");
        }

        @Override
        protected Bitmap doInBackground(String... params) {
          //your downloading code
        }

        /**
        * After completing background task Dismiss the progress dialog
        * **/
        protected void onPostExecute(Bitmap bitmap) {
            progressDialog.dismiss();


            }
        }
0
On

Check this...........

public interface AsyncTaskListener<Boolean> {
    public void onCompletingTask(Boolean result);
}

public class AsyncTaskGetApiKey extends AsyncTask<Void, Void, Boolean>{

    ArrayList<AsyncTaskListener<Boolean>> listeners;

    public AsyncTaskLoadingInSplash() {
        listeners = new ArrayList<AsyncTaskListener<Boolean>>();
    }

    public void addListener(AsyncTaskListener<Boolean> listener){
        listeners.add(listener);
    }

    public void removeListener(AsyncTaskListener<Boolean> listener){
        listeners.remove(listener);
    }


    @Override
    protected Boolean doInBackground(Void... params) {

        //do your activities here

        return true; // If everything is fine
    }

    @Override
    protected void onPostExecute(Boolean result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        for (AsyncTaskListener<Boolean> listener:listeners) {
            listener.onReceivingApiKey(result);
        }
    }
}

public class MyFagemnt extends Fragment
implements AsyncTaskListener<Boolean>{

    AsyncTaskGetApiKey task;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //......

        task = new AsyncTaskGetApiKey();
        task.addListener(this);
        //........
    }


    @Override
    public void onCompletingTask(Boolean result) {
        // your asyntask finished 
        // now close the progress bar
    }
}