'The child view set to app:layout_constraintTop_toBottomOf="parent" cannot be displayed

The child View of ConstraintLayout is set app:layout_constraintTop_toBottomOf="parent" to hide at the bottom of the screen. Just after entering the Activity, when it pops up at the bottom of the screen through animate().translationYBy() on the Android 7.1 virtual machine, nothing happens.

After clicking the desktop key or the recent task key, and then switching back to Activity, the child View does appear. But I don't have this problem on the Android 10 virtual machine.

The visibility and size of the childView are all correct at the beginning of the animation.

When I add android:layout_marginTop="-1px" this line of code to the childView to make the childView show 1 pixel at the bottom of the screen, everything is normal!

What went wrong? How can I modify?

Code show as below :

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <View
        android:id="@+id/childView"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        app:layout_constraintTop_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

childView.animate().translationYBy(childView.measuredHeight.toFloat()).setDuration(200)
            .setListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator?) {
                    isAnimating = true
                    isShowing = true
                }

                override fun onAnimationEnd(animation: Animator?) {
                    isAnimating = false
                }

                override fun onAnimationCancel(animation: Animator?) {
                    isAnimating = false
                }

                override fun onAnimationRepeat(animation: Animator?) {
                }
            }).start()
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
android {
    compileSdk 31

    defaultConfig {
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"
    }
}


Solution 1:[1]

The child View of ConstraintLayout is set app:layout_constraintTop_toBottomOf="parent" to hide at the bottom of the screen.

But there's no need for you to hide it at the screen's bottom since it's the only child. But if you insist, i suggest that you change this line

app:layout_constraintTop_toBottomOf="parent"

to

app:layout_constraintBottom_toBottomOf="parent" 

app:layout_constraintStart_toStartOf="parent" 

app:layout_constraintEnd_toEndOf="parent"

It'll works very fine...

Happy Coding

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chisom emmanuel