Open In App

Android Slide Up/Down in Kotlin

Last Updated : 23 Feb, 2021
Like Article

In Android Animations are the visuals that are added to make the user interface more interactive, clear and good looking.

In this article we will be discussing how to create a Slide Up/Down animation in Kotlin.

XML Attributes Description
android:duration It is used to specify the duration of animation
android:fromYDelta It is the change in Y coordinate to be applied at the start of the animation
android:toYDelta It is the change in Y coordinate to be applied at the end of the animation
android:id Sets unique id of the view

First step is to create a new Project in Android Studio. For this follow these steps:

  • Click on File, then New and then New Project and give name whatever you like
  • Then, select Kotlin language Support and click next button.
  • Select minimum SDK, whatever you need
  • Select Empty activity and then click finish.

After that, we need to design our layout. For that we need to work with the XML file. Go to app > res > layout and paste the following code:

Modify activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
                tools:context=".MainActivity" android:orientation="vertical">
            android:textStyle="bold" />
            android:text="Slide Up"
            android:layout_marginLeft="100dp" />
            android:text="Slide Down"
            android:textAllCaps="false" />

Add anim folder

In this folder, we will be adding the XML files which will be used to produce the animations. For this to happen, go to app/res right click and then select, Android Resource Directory and name it as anim.
Again right click this anim folder and select Animation resource file and name it as slide_up. Similarly, also create slide_down.xml and paste the following code.


<?xml version="1.0" encoding="utf-8"?>
            android:toYDelta="-100%" />


<?xml version="1.0" encoding="utf-8"?>
            android:toYDelta="0" />

Modify MainActivity.kt file

Open app/src/main/java/yourPackageName/MainActivity.kt and do the following changes:

package com.geeksforgeeks.myfirstKotlinapp
import android.os.Bundle
import android.view.animation.AnimationUtils
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        //setting buttons onClickListener
        slide_down.setOnClickListener {
            //adding our custom made animation xml file
            val animation = AnimationUtils.loadAnimation(
                this, R.anim.fade_out)
            //appending animation to textView
        slide_up.setOnClickListener {
            val animation = AnimationUtils.loadAnimation(
                this, R.anim.fade_in)

AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>

Run as emulator:

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads