EditText's TextWatcher validator
<p>I am doing a login <code>Activity</code> which basically has an <code>EditText</code> and two <code>Buttons</code>.
It looks like this:</p>

<p><img src="http://img249.imageshack.us/img249/9108/loginm.png" alt="http://img249.imageshack.us/img249/9108/loginm.png"></p>

<p>The <code>EditText</code> has a <code>TextWatcher</code> that validates the user with an <code>AsyncTask</code> that hits a webservice.</p>

<p>The code:</p>

<pre><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&lt;String, Void, Boolean&gt; {

        @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]);
        }
    }
}
</code></pre>

<p>My issues:</p>

<p>While the <code>AsyncTask</code> is working I want to show a ProgressBar inside the <code>EditText</code>.
I tried to do it using:</p>

<pre><code>mUserNameET.setCompoundDrawables(null, null, mProgressBar.getProgressDrawable(), null);
</code></pre>

<p>but it doesn't work.</p>

<p>And I am also a bit worried about creating new instances of the <code>AsyncTask</code>. Am I leaking memory?</p>
