Skip to content

Instantly share code, notes, and snippets.

@Turnsole
Created December 14, 2016 20:08
Show Gist options
  • Save Turnsole/90fffe0ebd441cecb65c01e128defa8b to your computer and use it in GitHub Desktop.
Save Turnsole/90fffe0ebd441cecb65c01e128defa8b to your computer and use it in GitHub Desktop.
Delay setting the error message on an EditText. Avoid leaking that View, despite delay.
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.widget.EditText;
import java.lang.ref.WeakReference;
/**
* Delay setting the error message on an EditText. Avoid leaking that View, despite delay.
* <p>
* Created by cmabee on 12/14/16.
*/
public class InputErrorHandler extends Handler {
public static final int SHOW_ERROR = 0;
public static final int SHOW_ERROR_DELAY = 1000;
private WeakReference <EditText> mEditText;
private WeakReference <String> mErrorText;
/**
* Create a Handler to delay setting the error message on an EditText.
*
* @param editText the EditText to display an error message
* @param errorText the text to be displayed as the error message
*/
public InputErrorHandler(@NonNull EditText editText, String errorText) {
mEditText = new WeakReference<>(editText);
mErrorText = new WeakReference<>(errorText);
}
@Override
public void handleMessage(Message msg) {
if (mEditText.get() == null) {
return;
}
if (msg.what == SHOW_ERROR) {
mEditText.get().setError(mErrorText.get());
} else {
mEditText.get().setError(null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment