androidkotlinandroid-motionlayout

Issue with TextView visibility when setting multi-line text in Android MotionLayout layout


I'm encountering an issue with a simple Android layout containing a Button and a TextView. Initially, the TextView is hidden, and upon clicking the button, I set its visibility to VISIBLE and populate it with text.

However, I'm facing a peculiar behavior: when I set a single-line text, the TextView appears correctly upon clicking the button. But when I set a multi-line text, the TextView doesn't show up on the first click; instead, I need to click the button twice to make it visible.

Here's a minimal example of my code:

main_activity.xml

<androidx.constraintlayout.motion.widget.MotionLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_main_scene"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.motion.widget.MotionLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button = findViewById<Button>(R.id.button)

        val textView = findViewById<TextView>(R.id.textView)

        button.setOnClickListener {

            //textView.setText("Some dummy text") // it works

            // not working
            textView.setText("Some dummy text jtext text text text texte text texte txjkk ggg gggg ggggg gg trdff rrrrrr rrrr")

            textView.visibility = View.VISIBLE
        }
    }
}

activity_main_scene.xml

<MotionScene 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@+id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="1000">
       <KeyFrameSet>
       </KeyFrameSet>
    </Transition>

    <ConstraintSet android:id="@+id/start">
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
    </ConstraintSet>
</MotionScene>

Any suggestions or solutions would be greatly appreciated. Thank you for your help!


Solution

  • I found a solution to the issue in this post. I fixed it by placing the TextView inside the scene and added motion:visibilityMode="ignore" like this:

    <MotionScene 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:motion="http://schemas.android.com/apk/res-auto">
    
        <Transition
            motion:constraintSetEnd="@+id/end"
            motion:constraintSetStart="@id/start"
            motion:duration="1000">
           <KeyFrameSet>
           </KeyFrameSet>
        </Transition>
    
        <ConstraintSet android:id="@+id/start">
            <Constraint
                android:id="@+id/textView"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:visibility="invisible"
                motion:visibilityMode="ignore"
                motion:layout_constraintBottom_toBottomOf="parent"
                motion:layout_constraintEnd_toEndOf="parent"
                motion:layout_constraintStart_toStartOf="parent" />
        </ConstraintSet>
    
        <ConstraintSet android:id="@+id/end">
        </ConstraintSet>
    </MotionScene>