Search code examples
androidprogress-bartranslationgoogle-translate

where and how to start and stop progress bar in my code


i have a translating app working fine, but i want to implement PROGRESS BAR to it when user press the Translate button, the Progress bar should start and visible and once translating complete, progress bar should stop and be invisible.

below is my translate button code method

     translateBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkInternetConnection()) {

                //if there is internet connection, get translate service and start translation.
                getTranslateService();
                translate();

            } else {

                //if not display "no internet connection" warning
                Toast.makeText(MainActivity.this, "Error! please check your internet connection and try again!!", Toast.LENGTH_LONG).show();

            }
            progressBar.setVisibility(View.VISIBLE);
        }
    });

i have set the progress Bar in my code but it keep spinning and not stop when translate is complete. i am confused where to put my visibility and invisible code line to.


Solution

  • You may use these methods for showing and hiding of progress dialog. Call these methods where you need.

     ProgressDialog progressDialog;
        public void showProgressDialog(String title, String msg, Context context) {
            progressDialog.setTitle (title);
            progressDialog.setMessage (msg);
            progressDialog.setCancelable (false);
            progressDialog.show ();
        }
    
        public void hideProgressDialog() {
            if (progressDialog != null) {
                if (progressDialog.isShowing ()) {
                    progressDialog.dismiss ();
                }
            }
        }
    

    According to your code perspective call these methods like below.

     translateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkInternetConnection()) {
                  showProgressDialog(Your title,Your message,getContext);
    
                 //if there is internet connection, get translate service and start translation.
                   getTranslateService();
                    translate();
                    hideProgressDialog();
                } else {
    
                    //if not display "no internet connection" warning
                    Toast.makeText(MainActivity.this, "Error! please check your internet connection and try again!!", Toast.LENGTH_LONG).show();
    
                }
    
            }
        });
    

    Moreover if you wants to show your progress bar do it like that

     translateBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (checkInternetConnection()) {
                       progressBar.setVisibility(View.VISIBLE);
    
                     //if there is internet connection, get translate service and start translation.
                       getTranslateService();
                        translate();
                        progressBar.setVisibility(View.GONE);
    
                    } else {
    
                        //if not display "no internet connection" warning
                        Toast.makeText(MainActivity.this, "Error! please check your internet connection and try again!!", Toast.LENGTH_LONG).show();
    
                    }
    
                }
            });