TextInputLayout Error right align TextInputLayout Error right align android android

TextInputLayout Error right align


So thanks to Mike's answer I was able to figure out the solution.

I created a custom class:

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

Now I simply replaced my TextInputLayout with the CustomTextInputLayout.Works like a charm. Thanks a lot Mike. I wish I could credit you more for this answer.


Here's a hack in Kotlin that doesn't depend on view hierarchy:

class CustomTextInputLayout @JvmOverloads constructor(    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : TextInputLayout(context, attrs, defStyleAttr) {    override fun setError(errorText: CharSequence?) {        super.setError(errorText)        with(findViewById<TextView>(R.id.textinput_error)) {            // this will work as long as errorView's layout width is            // MATCH_PARENT -- it is, at least now in material:1.2.0-alpha06            textAlignment = TextView.TEXT_ALIGNMENT_VIEW_END        }    }}


My custom class is in Kotlin

class CustomTextInputLayout(context: Context, attrs: AttributeSet) :    TextInputLayout(context, attrs) {    override fun setErrorEnabled(enabled: Boolean) {        super.setErrorEnabled(enabled)        if (!enabled) {            return        }        try {            val layout = this            val errorView : TextView = ((this.getChildAt(1) as ViewGroup).getChildAt(0) as ViewGroup).getChildAt(0) as TextView            (layout.getChildAt(1) as ViewGroup).layoutParams.width = LayoutParams.MATCH_PARENT            (layout.getChildAt(1) as ViewGroup).getChildAt(0).layoutParams.width = FrameLayout.LayoutParams.MATCH_PARENT            errorView.gravity = Gravity.END        } catch (e: Exception) {            e.printStackTrace()        }    }}