问题
I have created a sample project on github Please check it out to reproduce the problem I am facing.
Project Structure: I have basic TabLayout
and Viewpager
in MainActivity
. I also have FirstFragment
and SecondFragment
which are hosted by my TabLayout
. Everything fairly simple and straightforward there. Look at the screenshots:
Layout of MainActivity
has MotionLayout
as the root layout. When user moves from FirstFragment
to SecondFragment
, I increase the size of the CardView
. My transition code in transition.xml
:
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:duration="200"
motion:constraintSetEnd="@+id/end"
motion:constraintSetStart="@+id/start">
</Transition>
<ConstraintSet android:id="@+id/start">
<Constraint
android:id="@+id/cardView"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint
android:id="@+id/cardView"
android:layout_width="0dp"
android:layout_height="400dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
</ConstraintSet>
</MotionScene>
As you can see in the screenshots, SecondFragment
has TextInputLayout
and a continue Button
. When Button
is pressed, I check if the EditText
is empty or not. If so, I set error message to TextInputLayout
. When user enters some text and tries to continue again, I remove the error message from TextInputLayout
. But, problem is that the extra space (that is added by TextInputLayout
) to show error message is not being removed.
How I am removing the error message in MainActivity
:
btnSurnameContinue.setOnClickListener {
if(etSurname.text.isNullOrEmpty()) {
tilSurname.isErrorEnabled = true
tilSurname.error = "Oops empty detected"
} else {
tilSurname.error = ""
tilSurname.isErrorEnabled = false
}
}
I have test and this issue is happening in Android 9 and Android 8. I checked the same issue on Android 5.0 and it worked pretty fine. I think the problem is specific to Android 9 and Android 8. I have not tested in other versions though.
IMPORTANT NOTE 1: If I remove app:layoutDescription="@xml/transition"
from the MotionLayout
in activity_main.xml
, everything is working fine! But removing this means that there will be no animation. What is the point of using MotionLayout
then?
IMPORTANT NOTE 2: After the issue happens, if I swipe to FirstFragment
and come back to SecondFragment
, everything starts to work fine. The extra space (which was added because of the error message) will also disappear. Also, error messages (which did not appear at first) starts to appear again (see Additional Problems 2 and 3). It is very strange. I am thinking this swipe back to FirstFragment
and returning to SecondFragment
back is causing the SecondFragment
to re-render itself. Therefore, I am guessing everything starts to work/look correctly. What do you think? May be I am wrong here.
The problem gets even worse when you have mutiple TextInputLayout
s in the SecondFragment
. (I have only included one TextInputLayout
for simplicity in the sample project). So the problems are:
Additional Problems:
- When error is removed, the extra space added to show error is not dissappearing. (This is the main issue that I have explained above).
- Sometimes, error messages are not even being shown.
TextInputLayout
is colored with accent color only and no error message shown at all. - Resetting error message to some other lengthier message causes the error message to be shown partially. Full error message will never be shown.
Please, checkout the github sample project.
来源:https://stackoverflow.com/questions/59968915/transition-in-motionlayout-causing-textinputlayout-seterror-message-not-work-in