Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
Slide Animation in Android with Kotlin
Next article icon

Slide Animation in Android with Kotlin

Last Updated : 06 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Animations in Android allow UI elements to perform aesthetic moves across the screen. Animations can be implemented from a third party as well as developed from scratch. Despite the source, animations can be applied to any kind of view or UI element.

Slide Animation

Typically, we have demonstrated a slide animation, one from left to right and the other from right to left on two TextView. So in this article, we will show you how you could programmatically create slide animations from scratch in Android. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add two buttons and two TextViews for performing animations. TheTextView is initially set to invisible which on button click would be set to visible.

XML
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout      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"     tools:context=".MainActivity"     android:id="@+id/relative_layout_1">      <Button         android:id="@+id/button_1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="slide from left"         android:layout_centerHorizontal="true"         android:layout_marginTop="50sp"/>      <Button         android:id="@+id/button_2"         android:layout_below="@id/button_1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="slide from right"         android:layout_centerHorizontal="true"         android:layout_marginTop="50sp"/>      <TextView         android:id="@+id/text_view_1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerInParent="true"         android:visibility="invisible"         android:text="TextView1"         android:textSize="30sp"/>      <TextView         android:id="@+id/text_view_2"         android:layout_below="@id/text_view_1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerInParent="true"         android:visibility="invisible"         android:text="TextView2"         android:textSize="30sp"/>  </RelativeLayout> 

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin
package org.geeksforgeeks.slideanimation  import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.transition.Slide import android.transition.TransitionManager import android.view.Gravity import android.view.View import android.widget.Button import android.widget.RelativeLayout import android.widget.TextView  class MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          // Declaring and initializing          // the elements from the layout file         val mRelativeLayout = findViewById<RelativeLayout>(R.id.relative_layout_1)         val mButtonLeft = findViewById<Button>(R.id.button_1)         val mButtonRight = findViewById<Button>(R.id.button_2)         val mTextView1 = findViewById<TextView>(R.id.text_view_1)         val mTextView2 = findViewById<TextView>(R.id.text_view_2)          // When button is clicked, left          // to right animation is created         mButtonLeft.setOnClickListener {             val mSlideLeft = Slide()             mSlideLeft.slideEdge = Gravity.START             TransitionManager.beginDelayedTransition(mRelativeLayout, mSlideLeft)             mTextView1.visibility = View.VISIBLE         }          // When button is clicked, right         // to left animation is created         mButtonRight.setOnClickListener {             val mSlideRight = Slide()             mSlideRight.slideEdge = Gravity.END             TransitionManager.beginDelayedTransition(mRelativeLayout, mSlideRight)             mTextView2.visibility = View.VISIBLE         }     } } 

Output:

You can see that on button clicks, the respective animations are played.


Next Article
Slide Animation in Android with Kotlin

A

aashaypawar
Improve
Article Tags :
  • Kotlin
  • Android
  • Kotlin Android

Similar Reads

    Android Auto Image Slider with Kotlin
    Most e-commerce application uses auto image sliders to display ads and offers within their application. Auto Image Slider is used to display data in the form of slides. In this article, we will take a look at the Implementation of Auto Image Slider in our Android application using Kotlin. A sample v
    5 min read
    Android Animations in Kotlin
    Animation is a method in which a collection of images are combined in a specific way and processed then they appear as moving images. Building animations make on-screen objects seem to be alive. Android has quite a few tools to help you create animations with relative ease. so in this article we wil
    5 min read
    Android Rotate animations in Kotlin
    Rotate animation is a special kind of animation in Android which controls the Rotation of an object. These type of animations are usually used by developers to give a feel to the user about the changes happening in the application like loading content, processing data, etc. By using the rotate anima
    4 min read
    Android Slide Up/Down in Kotlin
    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 animati
    3 min read
    Fluid Slider Animations in Android
    Here we are going to see how we can implement Fluid Slider Animation in android studio using Kotlin. The idea to use this slider scale in our applications makes our UI look beautiful and more interactive. What is Fluid Slider Animation? A Fluid Slider is a slider widget with a popup bubble displayin
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences