0

I'm working on an Android app and am having trouble updating the GUI. Basically what I want to achieve is when my user clicks the Sign In button, call the setVisibility method on the groupLogInProgress as defined below and set it to View.VISIBILE. Then fire off my method that logs them in, and if it returns a success value, set the groupLogInProgress to View.GONE, and the groupLogInSuccess to View.VISIBLE (displays "Sign In Successful!") pause for a few seconds and start my main intent. If my log in method returns a false, have it set the groupLogInProgress to View.GONE and the groupLogInError to View.VISIBLE. I can't seem to figure out how to make these things happen without causing my app to hang while it waits for the log in method to complete.

Below is what I have so far, any help is GREATLY appreciated!!

//Hide all Sign In progress/success/error layouts onCreate
groupLogInProgress = (LinearLayout) findViewById(R.id.groupLoginProgress);
groupLogInSuccess = (LinearLayout) findViewById(R.id.groupLoginSuccess);
groupLogInError = (LinearLayout) findViewById(R.id.groupLoginError);        
hideAllStatus(); //this is simple method that sets all above groups to View.GONE

//Sign in button onClick handler
public void onClick(View v) { 
    loginData = LogInUser(username, password);
if(loginData == null)
{
    //set groupLogInError to View.VISIBLE, all others to GONE
}
else
{       
        //set groupLogInSuccess to View.VISIBLE, all others to GONE and pause for a few seconds to allow user to see "Sign In Successful!" message
    }
}    
Phil
  • 4,029
  • 9
  • 62
  • 107

1 Answers1

0

Define an AsyncTask:

private class LoginTask extends AsyncTask<Void, Integer, Integer>
{
 static final int STATUS_SUCCESS = 1;
 static final int STATUS_FAIL = 0;

 @Override
 protected void onPreExecute()
 {
  hideAllStatus(); //this is simple method that sets all above groups to View.GONE
 }

 @Override
 protected Integer doInBackground(Void... params) 
 {
  loginData = LogInUser(username, password);
  return (loginData == null ? STATUS_FAIL : STATUS_SUCCESS);
 }

 @Override
 protected void onPostExecute(Integer result)
 {
  if (result == STATUS_FAIL)
  {
    //set groupLogInError to View.VISIBLE, all others to GONE
  }
  else
  {       
    //set groupLogInSuccess to View.VISIBLE, all others to GONE and pause for a few seconds to allow user to see "Sign In Successful!" message
  }

 }
}

Execute the task:

new LoginTask().execute();

I've glossed over some of the details, but this class will need access to loginData, your View variables, etc, so could be a private class within your Activity. Some of the details left for you, like passing around results, etc

CSmith
  • 13,318
  • 3
  • 39
  • 42
  • Thank you CSmith, using your example I have been able to get the status indicators to display correctly, with one exception. How can I make it so the GUI is updated to show the correct Error or Success group, but with the error I want it to show, wait for 3 seconds and go away, and with the success, I want it to also show for 3 seconds, and then start new intent. When I use Thread.sleep(3000) it seems to wait until the 3 seconds elapses before it refreshes the GUI with the correct status group, – Phil May 02 '13 at 19:12
  • so in order words it waits 3 seconds, and THEN says "Sign In Successful!" and goes away very fast instead of showing "Sign In Successful" and waiting for 3 seconds before starting the new intent. – Phil May 02 '13 at 19:14
  • You can add a private class member variable to LoginTask to hold your error string. You should consider Toast message for your 3 second "Sign in Successful" message (though you don't have explicit control over the time it appears). You can launch the new Intent immediately, and the Toast will still appear topmost. – CSmith May 02 '13 at 20:19
  • Excellent suggestion CSmith! Thank you so much, I've implemented the Toast message for the confirmation Sign In Successful, and used the ProgressDialog for my Signing In... dialog. Working beautifully! – Phil May 03 '13 at 13:27