Search code examples
androidandroid-viewandroid-alertdialogandroid-progressbarandroid-runonuithread

how do I dismiss my progress dialog after the needed function in alert dialog is applied?


I have a link in my fragment, which shows " click to open dialog" ,upon clicking it, an alert dialog pops up, showing " do you want to change the view? " with yes and no notations. If I click no, nothing happens and dialog dismisses as expected. However, If I click yes, how do call a function to refresh my view and until my view is refreshed show a progress dialog? Here's my code so far, I dont know where to dismiss the progress dialog or make sure the view is refreshed, any clues? :

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Are you sure you want to refresh your view?");
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                //refreshview here ? 

                ProgressDialog progressDialog = ProgressDialog.show(getActivity(), "",
                        "Loading. Please wait...", true);
                progressDialog.show();

                progressDialog.setCancelable(false);
                progressDialog.setCanceledOnTouchOutside(false);
            }
        });
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();

Solution

  • Better to use AsyncTask here and in onPostExecute(...) dismiss your Progress Dialog

    Take a look demos

    1. Official Page
    2. Examples

    Once you get data refresh your View in onPostExecute(...)

    Edit:

     //Progress Dialog Show logic here
     Runnable r=new Runnable() {
            @Override
            public void run() {
                //Dismiss the Dialog and refresh your Views
            }
        };
    
        new Handler().postDelayed(r,10*1000);
    

    This code is execute after 10 sec.