Skip to content

Instantly share code, notes, and snippets.

@MensObscura
Last active April 6, 2016 09:34
Show Gist options
  • Save MensObscura/83abffb9adc10120f68cdc7478ef66b7 to your computer and use it in GitHub Desktop.
Save MensObscura/83abffb9adc10120f68cdc7478ef66b7 to your computer and use it in GitHub Desktop.
TextInputLayout with red state
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.support.design.widget.TextInputLayout;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
public class TextInputLayoutWithError extends TextInputLayout {
private Context mContext;
private String mErrorMessage;
public TextInputLayoutWithError(Context context) {
super(context);
mContext = context;
}
public TextInputLayoutWithError(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init(attrs);
}
public TextInputLayoutWithError(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
init(attrs, defStyleAttr);
}
private void init(AttributeSet attrs) {
init(attrs, 0);
}
private void init(AttributeSet attrs, int defStyleAttr) {
final TypedArray typedArray = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.TextInputLayout, defStyleAttr, 0);
try {
mErrorMessage = typedArray.getString(R.styleable.TextInputLayout_errorText);
} finally {
typedArray.recycle();
}
}
public void setToErrorView() {
final EditText editText = getEditText();
if (editText == null) {
return;
}
final int color = ContextCompat.getColor(mContext, R.color.color_pink);
//TODO améliorer ce système de MAJ graphique, apparemment c'est un manque du Framework:http://tinyurl.com/z3z62lp
//colore la ligne de l'éditext
editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
setHintTextAppearance(R.style.ErrorAppearanceTextInputLayout);
if (getHint() != null && getHint().length() > 0) {
editText.setHint(getHint());
setHint("");
editText.setHintTextColor(color);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
final CharSequence editextHint = editText.getHint();
if (editextHint != null && editextHint.length() > 0) {
setHint(editextHint);
editText.setHint("");
}
} else {
//keeping the animation of textinputlayout
postOnAnimationDelayed(new Runnable() {
@Override
public void run() {
if (getHint().length() > 0 && editText.getText().length() == 0) {
editText.setHint(getHint());
setHint("");
editText.setHintTextColor(ContextCompat.getColor(mContext, R.color.color_pink));
removeCallbacks(this);
}
}
}, 200);
}
}
});
}
}
public String getErrorMessage() {
return mErrorMessage == null ? "" : mErrorMessage;
}
public void setToDefaultView() {
final EditText editText = getEditText();
if (editText == null) {
return;
}
final int color = ContextCompat.getColor(mContext, R.color.color_separator);
editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
setHintTextAppearance(R.style.AppearanceTextInputLayout);
}
}
@MensObscura
Copy link
Author

Do not forget :

<resources>

    <declare-styleable name="TextInputLayout">
        <attr name="errorText" format="reference" />
    </declare-styleable>


</resources>

in attrs.xml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment