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

}

2 Answers2

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
  • 166
  • 1
  • 11
0

This worked for me i'm using material 'com.google.android.material:material:1.3.0' version

class CustomTextInputLayout(context: Context?, attrs: AttributeSet?) : TextInputLayout(
    context, attrs
) {
    var errorMessage: String?

    init {
        val attributes = context?.obtainStyledAttributes(attrs, R.styleable.TextInputLayout)
        errorMessage = attributes?.getString(R.styleable.TextInputLayout_errorMessage)
        attributes?.recycle()
    }


    override fun setErrorEnabled(enabled: Boolean) {
        super.setErrorEnabled(enabled)
        when {
            enabled -> {
                errorMessage?.let {
                    try {
                        val controller =
                            TextInputLayout::class.java.getDeclaredField("indicatorViewController")
                        controller.isAccessible = true
                        val fErrorView = controller.type.getDeclaredField("errorView")
                        fErrorView.isAccessible = true
                        val mErrorView = fErrorView[controller[this]] as TextView
                        mErrorView.apply {
                            text = errorMsg
                            setCompoundDrawablesWithIntrinsicBounds(
                            AppCompatResources.getDrawable(
                                context,
                                R.drawable.ic_icon_alert_error
                            ), null, null, null
                        )
                            compoundDrawablePadding = 10.px
                            gravity = Gravity.CENTER_VERTICAL
                            layoutParams =
                                FrameLayout.LayoutParams(
                                    LayoutParams.WRAP_CONTENT,
                                    LayoutParams.WRAP_CONTENT
                                )
                                    .apply {
                                        topMargin = 10.px
                                    }
                        }
                        error = errorMsg

                    } catch (e: Exception) {
                        e.printStackTrace() 
                    }
                }
            }
        }

    }
Soft Code
  • 49
  • 5