How To Make Error Message In Textinputlayout Appear In Center
Currently it looks as in attached with this layout:
Lesson learnt :-) is if any margins has to be applied to EditText, better same, if possible, can be applied to TextInputLayout to keep hint and error messages to be placed properly.
Solution 2:
classCenterErrorTextInputLayout(context: Context, attrs: AttributeSet) : TextInputLayout(context, attrs) {
overridefunsetErrorTextAppearance(resId: Int) {
super.setErrorTextAppearance(resId)
val errorTextView = this.findViewById<TextView>(R.id.textinput_error)
val errorFrameLayout = errorTextView.parent as FrameLayout
errorTextView.gravity = Gravity.CENTER
errorFrameLayout.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
}}
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.
publicclassTextInputLayout_Center : TextInputLayout
{
publicoverridevoidShowError(string text, Android.Graphics.Drawables.Drawable icon)
{
base.ShowError(text, icon);
centerErrorMessage(this);
}
voidcenterErrorMessage(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.
@OverridepublicvoidsetError(@Nullable CharSequence errorText) {
// allow android component to create error viewif (errorText == null) {
return;
}
super.setError(errorText);
// find this error view and calculate start margin to make it look like centeredfinalTextViewerrorTextInput= (TextView) findViewById(R.id.textinput_error);
errorTextInput.measure(0, 0);
interrorWidth= errorTextInput.getMeasuredWidth();
intlayoutWidth= getWidth();
LinearLayout.LayoutParamsparams= (LinearLayout.LayoutParams) errorTextInput.getLayoutParams();
params.setMarginStart((layoutWidth - errorWidth) / 2);
errorTextInput.setLayoutParams(params);
}
Solution 5:
@OverridepublicvoidsetErrorEnabled(boolean enabled) {
super.setErrorEnabled(enabled);
if (!enabled)
return;
try {
setErrorGravity(this);
} catch (Exception e) {
e.printStackTrace();
}
}
privatevoidsetErrorGravity(ViewGroup view)throws Exception {
for (inti=0; i < view.getChildCount(); i++) {
ViewerrorView= view.getChildAt(i);
if (errorView instanceof TextView) {
if (errorView.getId() == com.google.android.material.R.id.textinput_error) {
FrameLayouterrorViewParent= (FrameLayout) errorView.getParent();
errorViewParent.setLayoutParams(newLinearLayout.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"