Automatically show text of error in EditText - android

I'm using code like
EditText.setError("Something went wrong");
to show an error on an EditText. But only the exclamation mark icon is shown, not the text of the error. Clicking on it shows the error text.
How can I have the error text display automatically, without needing to touch the icon first?

You can set focus to the EditText so that the user doesn't have to click on it.
editText.requestFocus()
However, this would still only show one error at a time. I believe this is probably still the best way to go since that's the way the platform implements it.

// In you layout Xml
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:id="#+id/layFName"
android:textColorHint="#color/edittext_hintcolor"
app:hintTextAppearance="#style/TextAppearence.App.TextInputLayout"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_name_registration"
android:inputType="textPersonName"
android:hint="Hint Text"
/>
</android.support.design.widget.TextInputLayout>
// In java
private TextInputLayout mLayFName;
mLayFName = (TextInputLayout) findViewById(R.id.layFName);
for set error
mInputLayoutView.setError(msg);
EditText.getBackground().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);}

Related

Do not move hint to the top when calling setError() of TextInputLayout

I have a layout file with a TextInputLayout:
<android.support.design.widget.TextInputLayout
android:id="#+id/fullname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true"
app:errorTextAppearance="#style/Error">
<EditText
android:id="#+id/fullnameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Full Name"
android:inputType="textEmailAddress"
android:padding="26dp"/>
</android.support.design.widget.TextInputLayout>
When I call the setError() method on the TextInputLayout the hint goes to the top as if the TextInputLayout would have focus.
I would like to leave the hint untouched while setting the error. It should look something like this:
Any ideas how to build this layout?
you can do it with alternative way , you can call setError() on OnCreate()
mTextInputLayout = findViewById(R.id.fullname);
mTextInputLayout.setError("Enter Your Full Name");
and when you want to show the Error do this
mTextInputLayout.setErrorEnabled(true);
and this will fix your problem with the hint
if (mTextInputLayout.isErrorEnabled()){
mTextInputLayout.setHintEnabled(false);
}else {
mTextInputLayout.setHintEnabled(true);
}
You will need to implement your own MyTextInputLayout class extending from TextInputLayout.
In that class you will have to control when to show or not the hint, overriding the custom behavior that shows hint when error appears.
Nice start point in another answer from SO: https://stackoverflow.com/a/44521653/2174489

Show EditText error when user is not focused on that editText

I have an editText that currently shows an error popover.
editText.setError("You have done something wrong");
Now I need a workflow such that, when user clicks on a button elsewhere, I need to set an error on the editText. But right now, only an error icon is shown.
My question is, is it possible to make it show the whole error text without the user actually clicking (focusing) on it?
Solution 1
Call
editText.requestFocus()
before setting the error.
Solution 2
Use a TextInputLayout.
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
And in code
textInputLayout.setError("Error");
Solution 3
Custom view, up to you.
I use that library MaterialEditText which makes everything more simple... Everything is well explained in the wiki page.
Helper Text and Error Text
helper text:
app:met_helperText="Integer"
error text:
just use original setError(CharSequence error) in java code.
regex check:
validationEditText.isValid("\\d+");
regex validate with error text setting:
validationEditText.validate("\\d+", "Only Integer Valid!");

EditText added is not a TextInputEditText. Please switch to using that class instead

I'm using an EditText inside a TextInputLayout, but after upgrading the support library to 23.2.0, I get this warning in the logcat, What's the difference between a regular EditText and a TextInputEditText? I can't seem to find any documentation for it.
I was wondering this too, Daniel Wilson gathered the documentation, but to the untrained eye it doesn't mean much. Here's what it's all about: "extract mode" is referring to the type of view that's shown when the space is too small, for example landscape on a phone. I'm using Galaxy S4 with Google Keyboard as input method editor (IME).
Landscape UI without visible IME
Based on the focus (on Description) you can see TextInputLayout in action pushing the hint outside the editor. Nothing special here, this is what TextInputLayout is supposed to do.
Landscape UI editing empty Name field
Editing the Name you can see that the IME doesn't give you a hint of what you're editing.
Landscape UI editing empty Description field
Editing the Description you can see that the IME gives you a hint of what you're editing.
Layout XMLs
The difference between the two fields is their type EditText VS TextInputEditText. The important thing here is that TextInputLayout has the android:hint and not the wrapped EditText, this is the case when TextInputEditText's few lines of Java code makes a big difference.
Name field
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Item Name"
>
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</android.support.design.widget.TextInputLayout>
Description field
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Item Description"
>
<android.support.design.widget.TextInputEditText
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:minLines="4"
android:scrollbars="vertical"
/>
</android.support.design.widget.TextInputLayout>
There is no documentation for it, but the class is a regular EditText with a single extra feature:
Using this class allows us to display a hint in the IME when in 'extract' mode.
Specifically it sets the EditorInfo.hintText. You'll notice in the TextInputLayout class you can specify the hint and it's appearance rather than as part of the child EditText widget.
If you need to do that, you should use a TextInputEditText so it pays attention to the hint info you specified in the TextInputLayout.
They are essentially the same thing, but I think the TextInputEditText has more features and possibly attributes. I changed to the TextInputEditText and everything worked and looked as it did before with the standard EditText.
The only difference is that when your device is in landscape mode, TextInputEditText will show the hint, EditText won't.
I had this problem and just deleted this line in my xml file:
android: fitsSystemWindows = "true"
and the error disappeared.

How to show error text in a fragment?

I'm setting the EditText error message using setError, for example:
editTextName.setError("Please enter your name");
editTextName.requestFocus();
The above works when the text views are in the activity itself, ie. an error icon and the text balloon are shown.
However, when text views are in a fragment, only the error icon is shown.
What is the correct procedure to show the error text balloon too?
yourEdittext.setError("This field is required");
yourEdittext.requestFocus();
You should call request focus for this to get it work. Hope this helps.
<android.support.design.widget.TextInputLayout
android:layout_below="#+id/email_holder"
android:id="#+id/password_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
Try adding your layout like above.

Android EditText.setError not working in not focusable

How can I display a text error with setError in an EditText not focusable? It's important that users don't modify this EditText, it'll be modified for the application. Maybe I have another option different than focusable=false?
Now, I have it:
<EditText
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:ems="10"
android:focusable="false"
android:inputType="text"
android:onClick="onClick" />
Thanks a lot!
Finally I think it's not possible to do... Because the first necessity is to block the text, doing it with focusable=false or with a TextView, and it also blocks the functionality setError.
An editText with focusable = false I can get a right drawable (the default red exclamation mark) but without text. For this reason I finally added the text with a Toast.
It's not completelly that I wanted, but it's the most similar.
Thanks for your help!
Just use TextView and if you have an error somewhere then show image with drawableRight.
Here's the example of doingit programmatically: https://stackoverflow.com/a/7380789/3864698

Categories

Resources