How to show error text in a fragment? - android

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.

Related

Displaying keyboard below the the selected line using multi-line TextField

so I've got the following layout with the following TextInputEditText :
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/note"
android:layout_marginTop="#dimen/layout_margin_default"
app:startIconDrawable="#drawable/baseline_notes_24">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
/>
</com.google.android.material.textfield.TextInputLayout>
Right now, when I select to write inside the TextInputEditText the keyboard automatically displays below the TextInputEditText:
Now, what I want to is that rather displaying the keyboard below the TextInputEditText, I want the display to keyboard below the current "selected" line, which should look like this:
Example video how it should look like:
https://jumpshare.com/v/TAO6jo1jdnRZDhYyzrkY
How can this be solved in the right way?
I've tried using android:windowsoftinputmode, but I couldn't find the appropriate attribute.
I'm assuming there is not much leeway when it comes to the displaying the keyboard?

Show a drop down list below the edit text when edit text changes

I have an edit text for entering the location and based on the results of the google places API I have to show them in a dropdown list below the edit text.
Unable to figure out how to create a down list below the edit text.
Attaching image for reference
Sample image
try to use AutoCompleteTextView, add some API results adapter and refresh its content when any letter enters into EditText (use TextWatcher)
from AutoCompleteTextViews doc:
An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
One way to achieve your design
In XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/id_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/enter_building_or_street_address"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/id_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"/>
</LinearLayout>
In Class
Implement TextWatcher for the EditText
onTextChanged - call place api and list the response on
recyclerview if response is not null

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

Automatically show text of error in EditText

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

How to show error message under EditText in TextLayoutInput in Android?

For example I have a TextLayoutInput that allows user to put in password or email address\n
When the input type is not correct, i need the error message show up underneath the input line, like the design in here:
https://www.google.com/design/spec/patterns/errors.html#errors-user-input-errors
The xml code for TextLayoutInput is like this:
<TextInputLayout
android:id="#+id/password_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/password_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:hint="#string/password"
android:inputType="textPassword"
android:textAlignment="viewStart"
tools:ignore="UnusedAttribute"/>
</TextInputLayout>
How do i achieve this?
Thanks in advance!!!
It's easy. Read the section about Floating labels for editing text in Android Design Support Library Documentation. There it says:
In addition to showing hints, you can also display an error message
below the EditText by calling setError().
You can put a TextView under EditText and use .setText("your error message") to change it when an error occurs.

Categories

Resources