Untitled

mail@pastecode.io avatar
unknown
kotlin
7 months ago
1.7 kB
3
Indexable
Never
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.* // Import your layout's elements

class MainActivity : AppCompatActivity() {
    private var isLayoutVisible = false

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

        // Toggle the layout visibility when a button is clicked (you can use any trigger)
        toggleButton.setOnClickListener {
            if (isLayoutVisible) {
                hideLayout()
            } else {
                showLayout()
            }
        }
    }

    private fun showLayout() {
        animatedLayout.visibility = View.VISIBLE
        animatedLayout.alpha = 0f

        animatedLayout.animate()
            .alpha(1f)
            .setDuration(500) // Duration of the animation in milliseconds
            .setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator?) {
                    // Animation finished
                }
            })

        isLayoutVisible = true
    }

    private fun hideLayout() {
        animatedLayout.animate()
            .alpha(0f)
            .setDuration(500) // Duration of the animation in milliseconds
            .setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator?) {
                    animatedLayout.visibility = View.GONE
                }
            })

        isLayoutVisible = false
    }
}