Skip to content

Instantly share code, notes, and snippets.

@Sirelon
Created August 25, 2016 19:56
Show Gist options
  • Save Sirelon/24d272dd178e9d230431e11d7ebe8d7a to your computer and use it in GitHub Desktop.
Save Sirelon/24d272dd178e9d230431e11d7ebe8d7a to your computer and use it in GitHub Desktop.
EditText, with setErrot functionality without holding TextInputLayout. Just get parent, and try to setError for parent. If parent not instance of TextInputLayout than eror have been set to itself view.
package com.sirelon;
import android.content.Context;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.text.TextUtils;
import android.util.AttributeSet;
/**
* @author romanishin
* @since 18.05.16.
*/
public class AppTextInputEditText extends TextInputEditText {
public AppTextInputEditText(Context context) {
super(context);
}
public AppTextInputEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AppTextInputEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setError(CharSequence error) {
if (getParent() instanceof TextInputLayout) {
TextInputLayout textInputLayout = (TextInputLayout) getParent();
if (TextUtils.isEmpty(error)) {
textInputLayout.setErrorEnabled(false);
}
textInputLayout.setError(error);
} else {
super.setError(error);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment