-2

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();
    }
}

}

1 Answers1

1

I suppose you are doing it wrong. There is field named indicatorViewController that control error and helper text showing. The version is material:1.1.0-alpha02. Check the source code of TextInputLayout.

There is error text appearance. You can change the gravity by style (R.styleable.TextInputLayout_errorTextAppearance).

Madina Saidova
  • 168
  • 1
  • 9