Android Slide Up/Down in Kotlin

Android Slide Up/Down in Kotlin

Animating views to slide up or down can create a dynamic user experience in Android apps. Let's see how to create a slide-up and slide-down animation in Kotlin for Android.

1. Create Animation Resources

Firstly, you need to define the slide-up and slide-down animations in XML:

res/anim/slide_up.xml:

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromYDelta="100%p" android:toYDelta="0%p" android:duration="500" /> 

res/anim/slide_down.xml:

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromYDelta="0%p" android:toYDelta="100%p" android:duration="500" /> 

2. Apply the Animations in Kotlin:

In your Kotlin code (perhaps in an Activity or Fragment), you can load and start the animations as follows:

import android.os.Bundle import android.view.animation.AnimationUtils import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Load animations val slideUp = AnimationUtils.loadAnimation(this, R.anim.slide_up) val slideDown = AnimationUtils.loadAnimation(this, R.anim.slide_down) // Assuming you have a button or some other view named 'myButton' // that you want to animate: myButton.setOnClickListener { if (viewIsVisible) { myView.startAnimation(slideDown) myView.visibility = View.GONE } else { myView.startAnimation(slideUp) myView.visibility = View.VISIBLE } viewIsVisible = !viewIsVisible // Toggle view state } } private var viewIsVisible = true } 

Note: Make sure you have the myView view defined in your XML layout which you want to slide up or down. The viewIsVisible variable is just a simple flag to track the visibility state of the myView.

3. Kotlin Extension (Optional):

To make the code more idiomatic to Kotlin, you can define extension functions:

fun View.slideUp() { val slideUp = AnimationUtils.loadAnimation(context, R.anim.slide_up) startAnimation(slideUp) visibility = View.VISIBLE } fun View.slideDown() { val slideDown = AnimationUtils.loadAnimation(context, R.anim.slide_down) startAnimation(slideDown) visibility = View.GONE } // Usage: myView.slideUp() // to slide up myView.slideDown() // to slide down 

This approach allows you to easily use slideUp() or slideDown() on any view.

Examples

  1. Slide up and slide down view animation in Android Kotlin:

    Use TranslateAnimation for sliding views:

    val slideUp = TranslateAnimation(0f, 0f, 0f, -view.height.toFloat()) slideUp.duration = 500 view.startAnimation(slideUp) 

    For sliding down, adjust the TranslateAnimation parameters.

  2. Sliding view pager animation in Android using Kotlin:

    Apply a slide animation to ViewPager fragments:

    viewPager.setPageTransformer { page, position -> val translationX = page.width * -position page.translationX = translationX } 
  3. Animate fragment slide in Android with Kotlin:

    Use FragmentTransaction for fragment transitions with slide animations:

    val transaction = supportFragmentManager.beginTransaction() transaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out) transaction.replace(R.id.container, newFragment) transaction.addToBackStack(null) transaction.commit() 
  4. Slide transition animation between activities in Android Kotlin:

    Utilize overridePendingTransition for slide animations between activities:

    startActivity(intent) overridePendingTransition(R.anim.slide_in, R.anim.slide_out) 

More Tags

mandrill word-frequency syntax-error request linkedhashmap resteasy zend-framework2 bigdecimal multi-touch spyder

More Programming Guides

Other Guides

More Programming Examples