Question

I have a SignUp function, called when a button is clicked. In the onClickListener function, it is called like this:

SignUp(Uname.getText().toString(), Pword.getText().toString(),
        Umail.getText().toString());                     

While the function is running, I need to show a progress bar. How can I do this?

Here is the code for the SignUp() function:

public void  SignUp (String Username, String Password, String Email) {
    Integer Userch = CheckUName (Username);
    Integer UserMl = CheckUMail (Email);

    if (Userch == 3)
    {
        Toast.makeText(getActivity(), "این نام کاربری قبلا ثبت شده است",
                Toast.LENGTH_LONG).show();      
    }
    else if (UserMl == 3)
       {
            Toast.makeText(getActivity(), "این پست الکترونیکی قبلا ثبت شده است", Toast.LENGTH_LONG).show();     
       }
       else if ((UserMl == 4) && (Userch == 4))
       {

                String Pass = null;
                InputStream is = null;
                String result = "";
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("UUName", Username));
                nameValuePairs.add(new BasicNameValuePair("UPWord", Password));
                nameValuePairs.add(new BasicNameValuePair("UMail", Email));

                try{

                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(SignupAddress);
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        is = entity.getContent();
                        Toast.makeText(getActivity(), "ثبت نام انجام شد", Toast.LENGTH_LONG).show();        

                }catch(Exception e){

                    Log.e("log_tag", "Error in http connection "+e.toString());
                } 
       }


    }   

edited for libin : how i call :

 public void onClick(View v)
            {

                mProgressDialog.show();

                StrictMode.setThreadPolicy(policy); 

                String Result = login(Username.getText().toString());

                if (Username.getText().toString().equals(""))
                {

                    Toast.makeText(getActivity(), "نام کاربری را وارد کنید", Toast.LENGTH_SHORT).show();
                    mProgressDialog.hide();

                }
                else if  (Password.getText().toString().equals(""))
                {

                    Toast.makeText(getActivity(), "کلمه عبور را وارد کنید", Toast.LENGTH_SHORT).show();     
                    mProgressDialog.hide();

                }
                else
                {

                        if (Result.equals(Password.getText().toString()))
                         {

                            Intent i = new Intent(getActivity(), HomePage.class);
                             i.putExtra("new_variable_name",Username.getText().toString());
                             startActivity(i);

                         }
                         else if (Result == "Wrong Username or Password")
                         {
                            mProgressDialog.hide();

                              Toast.makeText(getActivity(), "نام کاربری یا کلمه عبور اشتباه است", Toast.LENGTH_SHORT).show();       
                         }
                          else if (Result == "Error Connection")
                         {
                            mProgressDialog.hide();

                              Toast.makeText(getActivity(), "مشکلی در برقراری ارتباط وجود دارد", Toast.LENGTH_SHORT).show();        
                         }
                          else if (Result == "Convert Error")
                         {
                            mProgressDialog.hide();

                              Toast.makeText(getActivity(), "مشکلی بوجود آمده است", Toast.LENGTH_SHORT).show();     
                         }
                          else 
                         {
                            mProgressDialog.hide();

                              Toast.makeText(getActivity(), "نام کاربری یا کلمه عبور اشتباه است", Toast.LENGTH_SHORT).show();       
                          }
                    }


            }

        });
Was it helpful?

Solution

Create an instance of ProgressDialog in onCreate() of Activity or Fragment

  private ProgressDialog mProgressDialog;

  mProgressDialog = new ProgressDialog(getActivity());
  mProgressDialog.setMessage("Signing In...");

When you click the button call,

  mProgressDialog.show();

And , When you get the response back from HTTP Request , call

 mProgressDialog.hide();

And in Activity onDestroy , dismiss the dialog

@Override
protected void onDestroy(){
    if (mProgressDialog != null) {
        mProgressDialog.dismiss();
        mProgressDialog = null;
    }
    super.onDestroy();
}

If your are execute the network request in an AsyncTask . You can show the progress in onPreExecute() and hide inonPostExecute()`

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top