I am doing a login Activity
which basically has an EditText
and two Buttons
.
It looks like this:
The EditText
has a TextWatcher
that validates the user with an AsyncTask
that hits a webservice.
The code:
public class Login extends Activity {
private EditText mUserNameET;
private Drawable mCorrect;
private Drawable mIncorrect;
private ProgressBar mProgressBar;
private MyApp mApp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
mApp = (MyApp) getApplication();
mUserNameET = (EditText) findViewById(R.id.user_name_et);
mUserNameET.addTextChangedListener(new LoginValidator());
mCorrect = getResources().getDrawable(R.drawable.correct);
mIncorrect = getResources().getDrawable(R.drawable.incorrect);
mProgressBar = new ProgressBar(this);
}
private class LoginValidator implements TextWatcher {
private ValidateUserAsyncTask validator = new ValidateUserAsyncTask();
@Override
public void afterTextChanged(Editable s) {
String value = s.toString();
if ( value.length() == 0 )
return;
/* If it's running, cancel it. */
/* Issue #2 */
if ( AsyncTask.Status.RUNNING == validator.getStatus() ) {
validator.cancel(true);
validator = new ValidateUserAsyncTask();
} else if ( AsyncTask.Status.FINISHED == validator.getStatus() ) {
validator = new ValidateUserAsyncTask();
}
validator.execute(value);
/* Issue #1 */
mUserNameET.setCompoundDrawables(null, null, mProgressBar.getProgressDrawable(), null);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}
class ValidateUserAsyncTask extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if ( result ) {
mUserNameET.setCompoundDrawablesWithIntrinsicBounds(null, null, mCorrect, null);
} else {
mUserNameET.setCompoundDrawablesWithIntrinsicBounds(null, null, mIncorrect, null);
}
}
@Override
protected Boolean doInBackground(String... params) {
return mApp.validateUser(params[0]);
}
}
}
My issues:
While the AsyncTask
is working I want to show a ProgressBar inside the EditText
.
I tried to do it using:
mUserNameET.setCompoundDrawables(null, null, mProgressBar.getProgressDrawable(), null);
but it doesn't work.
And I am also a bit worried about creating new instances of the AsyncTask
. Am I leaking memory?