0
0
Android Kotlinmobile~5 mins

Transition animations in Android Kotlin

Choose your learning style9 modes available
Introduction

Transition animations make moving between screens smooth and nice to look at. They help users understand changes in the app.

When switching from one screen to another in an app.
When showing or hiding parts of the screen with animation.
When you want to make your app feel more friendly and polished.
When guiding users' attention to important changes on the screen.
Syntax
Android Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_main)
  overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim)
}
Use overridePendingTransition right after startActivity or in onCreate to set animations.
Animations are defined in XML files inside res/anim/ folder.
Examples
This makes the new screen slide in from the right and the old screen slide out to the left.
Android Kotlin
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
This uses built-in Android fade animations for smooth fading between screens.
Android Kotlin
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
Start a new screen and apply zoom in/out animations for transition.
Android Kotlin
startActivity(intent)
overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out)
Sample App

This app has two screens. When you tap the button on the first screen, it opens the second screen with a slide animation from right to left.

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

    val button = findViewById<Button>(R.id.button)
    button.setOnClickListener {
      val intent = Intent(this, SecondActivity::class.java)
      startActivity(intent)
      overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left)
    }
  }
}

class SecondActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_second)
  }
}
OutputSuccess
Important Notes

Animations must be defined as XML files in res/anim/ folder.

Use simple animations like slide, fade, or zoom for best user experience.

Test animations on different devices to ensure smoothness.

Summary

Transition animations improve app feel by smoothing screen changes.

Use overridePendingTransition after starting a new activity.

Animations are XML files placed in res/anim/ folder.