I wants to make my TextInputlayout setError in right side of the view. I tried with CustomTextInputlayout.TextInputLayout.class.getDeclaredField("mErrorView") throws exception. MyProject having androidX library.
java.lang.NoSuchFieldException: No field mErrorView in class Lcom/google/android/material/textfield/TextInputLayout;
public class CustomTextInputLayout extends TextInputLayout {
public CustomTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setErrorEnabled(boolean enabled) {
super.setErrorEnabled(enabled);
if (!enabled) {
return;
}
try {
Field errorViewField = TextInputLayout.class.getDeclaredField("mErrorView"); // Getting exception here
errorViewField.setAccessible(true);
TextView errorView = (TextView) errorViewField.get(this);
if (errorView != null) {
errorView.setGravity(Gravity.RIGHT);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.END;
errorView.setLayoutParams(params);
}
} catch (Exception e) {
// At least log what went wrong
e.printStackTrace();
}
}
}