Skip to content Skip to sidebar Skip to footer

How To Make Error Message In TextInputLayout Appear In Center

Currently it looks as in attached with this layout: Copy

Solution 3:

Here a solution that always centers the errormessage no matter how big your TextInputLayout is.

You make a own class that inherits from TextInputLayout. Then override the ShowError(string text, Drawable icon) method. If the error is called you center the textView with the error.

    public class TextInputLayout_Center : TextInputLayout
    {
        public override void ShowError(string text, Android.Graphics.Drawables.Drawable icon)
        {
            base.ShowError(text, icon);

            centerErrorMessage(this);
        }

        void centerErrorMessage(ViewGroup view)
        {
            for (int i = 0; i < view.ChildCount; i++)
            {
                View v = view.GetChildAt(i);
                if (v.GetType() == typeof(TextView))
                {
                    v.LayoutParameters = new LayoutParams(ViewGroup.LayoutParams.MatchParent, v.LayoutParameters.Height);
                    ((TextView)v).Gravity = GravityFlags.CenterHorizontal;
                    ((TextView)v).TextAlignment = TextAlignment.Center;
                }
                if (v is ViewGroup)
                {
                    centerErrorMessage((ViewGroup)v);
                }
            }
        }
    }

Solution 4:

I achieved it by setting start margin for the error view. Below you can see my overriden version of setError method of the TextInputLayout component.

@Override
public void setError(@Nullable CharSequence errorText) {
    // allow android component to create error view
    if (errorText == null) {
        return;
    }
    super.setError(errorText);
    // find this error view and calculate start margin to make it look like centered
    final TextView errorTextInput = (TextView) findViewById(R.id.textinput_error);
    errorTextInput.measure(0, 0);
    int errorWidth = errorTextInput.getMeasuredWidth();
    int layoutWidth = getWidth();
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) errorTextInput.getLayoutParams();
    params.setMarginStart((layoutWidth - errorWidth) / 2);
    errorTextInput.setLayoutParams(params);
}

Solution 5:

@Override
public void setErrorEnabled(boolean enabled) {
    super.setErrorEnabled(enabled);
    if (!enabled)
        return;
    try {
        setErrorGravity(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void setErrorGravity(ViewGroup view) throws Exception {
    for (int i = 0; i < view.getChildCount(); i++) {
        View errorView = view.getChildAt(i);
        if (errorView instanceof TextView) {
            if (errorView.getId() == com.google.android.material.R.id.textinput_error) {
                FrameLayout errorViewParent = (FrameLayout) errorView.getParent();
                errorViewParent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                ((TextView) errorView).setGravity(Gravity.RIGHT);
                ((TextView) errorView).setTypeface(FontUtils.getTypeFace(view.getContext(), FontUtils.FONT_NAZANIN_TAR));
            }
        }
        if (errorView instanceof ViewGroup) {
            setErrorGravity((ViewGroup) errorView);
        }
    }
}

Post a Comment for "How To Make Error Message In TextInputLayout Appear In Center"