TextInputLayout, setError increases height - android

I have a simple TextInputLayout:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tilPwd"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Passcode"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" >
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
Then I have other views below.
The issue is that when I set the error on the TextInputLayout the height increases and pushes all the views below.
I tried a workaround setting a blank helperText, but when in talkback the blank helperText will be read and that's not what I want.
Is there a clean solution other then wrapping the TextInputLayout in some ViewGroup with a set height?

After a day of tries, I actually found the solution:
public void setHelperTextEnabled (boolean enabled)
Whether the helper text functionality is enabled or not in this layout. Enabling this
functionality before setting a helper message via
setHelperText(CharSequence) will mean that this layout will not change
size when a helper message is displayed.
Basically it occupies in advance the space of the helperText (even if it's not set), and that's the same space occupied from the error.
Worth to notice that the equivalent xml attribute "app:helperTextEnabled" does not solve the issue.

Yes, When you enable the error attribute of TextInputLayout, it will always increase the height of the View to show the error(whenever occurred), which will cause other Views/ViewGroups to push further below.
What you can do to get rid of the extra space is set the error programmatically.
For example, whenever an error occurred(or whenever if you want to set the error) you can call
setErrorEnabled(boolean value)
so that it can also show the error and will push other views down (which are below it), and if the error is gone(or you want to hide error), then you can call
setError("")
where you need to pass an empty string and then call the setErrorEnabled(false), so the textInputLayout will come back to its original position.

I had the same problem and solved it by adding this XML attribute.
app:errorEnabled="true"

Related

Text clipped by padding in big outlined TextInputLayout

One of my apps contains some kind of "notes field", which is a big textinputlayout containing a potentially big text. I'm using this piece of layout (i removed some less relevant attributes):
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginVertical="#dimen/activity_vertical_margin"
android:clipChildren="false">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|start"
android:maxHeight="#dimen/notes_maximum_height"
android:minHeight="#dimen/notes_minimum_height"
android:inputType="textMultiLine|textCapSentences"
android:paddingHorizontal="0dp"
android:paddingVertical="8dp" />
</com.google.android.material.textfield.TextInputLayout>
I would like a vertical padding, so that the cursor and the first line don't "touch" the outline, and that's ok. The problem is, when the content reaches its maximum height and starts scrolling, i can see the text clipped by the padding and not touching the upper and lower part of the outline. So i tried with clipChildren set to false as i often do, but i had no luck. Same behavior. After a lot of failed attempts, i'm really wondering if this is even possible!
I wasn't able to find a real answer to this question. It seems i'm the only one with this problem, for some reason. The workaround i used is a combination of a smaller padding and a fade out effect on the edittext. So i simply added the following to the edittext:
android:paddingVertical="4dp"
android:requiresFadingEdge="vertical"
This is not what i wanted, but i tried solving this apparently simple problem for months and it's time to move on :D

Android: TextInputEditText showing text only after change focus

I have a big problem with a EditText that does not show any input until i change focus from element. When I click on the input, the keyboard appears and while i type, there is nothing displayed in the edit text view. The input is shown only when i close the keyboard
It is not a problem of colours. I mention that the view are contained in a constraint layout. I can enable hardware acceleration but this will slow down the application very much and it's not an option. I can however discard the constraint layout and change it to a linear layout but this will affect the rest of the code. I am sure that I am missing something, an easy fix, but i can't put my finger on it. Thanks a lot in advance
<android.support.design.widget.TextInputLayout
android:id="#+id/codeEditTextContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toTopOf="#id/verifyButton"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<android.support.design.widget.TextInputEditText
android:id="#+id/codeEditText"
android:layout_width="match_parent"
android:inputType="text"
android:layout_height="wrap_content"
android:hint="#string/sms_code"
android:padding="10dp"
android:textColor="#color/white" />
</android.support.design.widget.TextInputLayout>
!!UPDATE
Found the problem, but still i have no answer. This problem is present only on Android Pie and only when the edit text is in the lower part of the screen and has to be raised by the soft keyboard. I already tried to add softInputWindowMode to adjustResize or adjustPan but this is not doing anything.
The problem itself is a duplicate to this question Android Pie edittext does not adjustPan/resize while typing
which still doesn't have an answer
The problem man be about hardwareAccelerated. I was having the same problem and waste too much time to solve before saw this page.
Android Pie edittext does not adjustPan/resize while typing

Accessibility Checks - TextInputEditText has contradictory exceptions

All, we're using the Espresso Accessibility Checking and are getting the following errors for the same view. What's the best way to fix this? Removing the content description fixes the second error but the first still remains. Is there a way to satisfy the Accessibility Checker for TextInputEditText?
https://developer.android.com/training/testing/espresso/accessibility-checking
AccessibilityViewCheckException: There were 2 accessibility errors:
TextInputEditText{id=2131362592, res-name=reason}: View is missing speakable text needed for a screen reader
TextInputEditText{id=2131362592, res-name=reason}: Editable TextView should not have a contentDescription.
I noticed that this error will go away if you set the hint in the TextInputEditText and remove the labelFor in TextInputLayout.
According to Google documentation, setting the hint is the way to tell Accessibility about your view for EditTexts.
When labeling editable elements, such as EditText objects, use the android:hint XML attribute for static elements and the setHint() method for dynamic elements to indicate each element's purpose. (Source)
Also, in Google's example of TextInputLayout, it does not specify that you need to have the labelFor field. (Source)
Code Example:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/edit_text_hint"/>
</android.support.design.widget.TextInputLayout>
I am aware that this post is in direct contradiction to this post:
Accessibility Check fail when using TextInputLayout. However, doing the things mentioned in that post did not work for me on the latest version of Android.

How to get Textinputlayout hint android in multiple lines?

I have tried many options available online but none of them seem to be working.
I have used this XML for this purpose.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#style/TextLabel">
<com.projects.fonts.RobotoEditText
style="#style/EditText"
android:hint="#string/description"
android:singleLine="false"
app:font="#string/roboto_regular" />
</android.support.design.widget.TextInputLayout>
I have also set
<item name="android:singleLine">false</item>
in both TextLabel and EditText. But none of them are working.
You can't.
TextInputLayout uses CollapsingTextLayout which measures the hint as a CharSequence:
mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length())
CharSequence has no notion of a line, thus you can't add more.
It's not quite a hint if it's so long.
If you still want to display it consider adding a separate TextView below/above/over and animating it (if want/need to).
You can do this with TextInputLayout and TextInputEditText.
In the TextInputEditText set the hint as you normally would (android:hint="Hint Text").
In the TextInputLayout you must disable the hint by setting "app:hintEnabled="false".
Notes:
If you leave hintEnabled to true (true by default) the hint will be single line and turns into a label when the user taps the EditText giving it focus.
If you change hintEnabled to false this feature is removed and the EditText shows the hint with multiple lines as intended. It does not turn into a label and disappears once the user starts actually typing.
Example code below:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:hintEnabled="false"
app:boxBackgroundMode="none">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your long hint"/>
</com.google.android.material.textfield.TextInputLayout>
In contrast to simas answer it seems that there is a workaround. You can achieve the expected behaviour by setting the hint programmatically in the TextInputEdittext. The downhill is that the floating label does not work after that, by I believe that it is not something that we expect with so long hint.
Important note: If we set the hint to the TextInputLayout, then it will not work.

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.

Categories

Resources