Jordan Hochstetler
Jordan Hochstetler

Reputation: 1416

Android - Using Indeterminate ProgressBar with another Thread

I am trying to use the indeterminate progress bar from when the user clicks a button to when the email is sent. I am having problems with the thread that I open up to send the email and the progress bar I think. Right now it just crashes when the two are close together and I wasn't sure if there is a smart way to enable the progress bar (basically I just want the animation of the swirling circle while the emails are sent so that the user has some feedback that something is happening in the background).

OnCreate

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    setContentView(R.layout.signin_page);

    verify_button = (Button) findViewById(R.id.verify_button);
    signin_button = (Button) findViewById(R.id.signin_button);

    staysignedin = (CheckBox) findViewById(R.id.staysignedinCheck);

    link4help = (TextView) findViewById(R.id.link_to_register);

    gmail = (EditText) findViewById(R.id.signin_email);
    password = (EditText) findViewById(R.id.signin_password);

    email_success = (ImageView) findViewById(R.id.email_authenticate_success);
    password_success = (ImageView) findViewById(R.id.password_authenticate_success);

    email_success.setVisibility(View.INVISIBLE);
    password_success.setVisibility(View.INVISIBLE);

    signin_button.setEnabled(false);

    verify_button.setOnClickListener(this);
    signin_button.setOnClickListener(this);
    link4help.setOnClickListener(this);

    final float scale = this.getResources().getDisplayMetrics().density;
    staysignedin.setPadding(staysignedin.getPaddingLeft() + (int)(10.0f * scale + 0.5f),
            staysignedin.getPaddingTop(),
            staysignedin.getPaddingRight(),
            staysignedin.getPaddingBottom());

     /* Setting up handler for ProgressBar */
    //b = (ProgressBar) findViewById(R.id.progressBar_verify);
    setProgressBarIndeterminateVisibility(false);

}

OnClick

@Override
public void onClick(View v) {
    setProgressBarIndeterminateVisibility(true);
    //Log.e("verify clicked","hi");
    switch(v.getId()){


    case R.id.verify_button:

        Thread thread = new Thread(){
            public void run(){


                String gmailString = gmail.getText().toString();
                String passString = password.getText().toString();
                String[] recip = new String[]{gmailString};
                String body = "\nThis is a test for the amazing Dictation2Go App!   Created by --";
                    MailAccount a  = new MailAccount(gmailString,passString);
                    try {
                        isGoogleAuthenticated = a.sendEmailTo(recip, "test", body);
                    } catch (MessagingException e) {
                           Log.e("failed to connect", "mess: "+e.getMessage());
                           isGoogleAuthenticated = false;
                    }               
            }
        };
        thread.start();

        try {
            thread.join();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }


        Log.e("END RESULT",String.valueOf(isGoogleAuthenticated));

        if(isGoogleAuthenticated){
            pb.setVisibility(View.INVISIBLE);
            email_success.setVisibility(View.VISIBLE);
            password_success.setVisibility(View.VISIBLE);
            signin_button.setEnabled(true);
            password.setEnabled(false);
            gmail.setEnabled(false);

            gmail.setBackgroundResource(R.layout.bordersuccess);
            password.setBackgroundResource(R.layout.bordersuccess);
        }else{
            gmail.setText("");
            password.setText("");
        }
        setProgressBarIndeterminateVisibility(false);
        break;

----- Full Solution ---------------------------------------------------------

in OnCreate Method of the Class

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.signin_page);
    setProgressBarIndeterminateVisibility(false); //Sets Default value
    //LOOK at Picture #1 for my screen in this state

}

in OnClick method (I implemented setOnClickLister for this activity)

@Override
public void onClick(View v) {

    setProgressBarIndeterminateVisibility(true);

    switch(v.getId()){


    case R.id.verify_button:


        String gmailString = gmail.getText().toString();
        String passString = password.getText().toString();

        new AsyncTask<String,Void,Boolean>() {
            protected void onPreExecute() {
                setProgressBarIndeterminateVisibility(true);
                        //LOOK at Picture #2 for my screen in this state
            }

            protected Boolean doInBackground(String... args) {
                String gmailString = args[0];
                String[] recip = new String[] { gmailString };
                String passString = args[1];
                String body = args[2];

                MailAccount a  = new MailAccount(gmailString, passString);
                try {
                    return a.sendEmailTo(recip, "test", body);
                } catch (MessagingException e) {
                    Log.e("failed to connect", "mess: "+e.getMessage());
                    return false;
                } 
            }

            protected void onPostExecute(Boolean isGoogleAuthenticated) {
                setProgressBarIndeterminateVisibility(false);
                if(isGoogleAuthenticated){
                    email_success.setVisibility(View.VISIBLE);
                    password_success.setVisibility(View.VISIBLE);
                    signin_button.setEnabled(true);
                    password.setEnabled(false);
                    gmail.setEnabled(false);

                    gmail.setBackgroundResource(R.layout.bordersuccess);
                    password.setBackgroundResource(R.layout.bordersuccess);
                }else{
                    gmail.setText("");
                    password.setText("");
                }
            }
        }.execute(gmailString, passString, "test complete");
        //LOOK at Picture #3 for my screen in this state

        break;

Picture #1 onCreate

Picture #1 onCreate


Picture #2 onClick - Loading

Picture #2 onClick - Loading


Picture #3 Finished Loading

Picture #3 Finished Loading

Upvotes: 1

Views: 1348

Answers (1)

marcus.ramsden
marcus.ramsden

Reputation: 2633

In your onClick method you will want to use an AsyncTask. Using AsyncTask means that you don't need to worry about writing your own threading code. As noted in the comments the join that you are doing will block the main thread, potentially causing your app to stop responding. Perhaps you will want something along the lines of;

@Override
public void onClick(View v) {
    String gmailString = gmail.getText().toString();
    String passString = password.getText().toString();

    new AsyncTask<String,Void,Boolean>() {
        protected void onPreExecute() {
            setProgressBarIndeterminateVisibility(true);
        }

        protected Boolean doInBackground(String... args) {
            String gmailString = args[0];
            String[] recip = new String[] { gmailString };
            String passString = args[1];
            String body = args[2];

            MailAccount a  = new MailAccount(gmailString, passString);
            try {
                return a.sendEmailTo(recip, "test", body);
            } catch (MessagingException e) {
                Log.e("failed to connect", "mess: "+e.getMessage());
                return false;
            } 
        }

        protected void onPostExecute(Boolean isGoogleAuthenticated) {
            setProgressBarIndeterminateVisibility(false);
            // Do the rest of your UI updates here
        }
    }.execute(gmailString, passString, body);
}

Upvotes: 3

Related Questions