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 fade_in. Similarly, also create fade_out.xml and paste the following code.
fade_in.xml 
 

XML




<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    android:interpolator="@android:anim/linear_interpolator">
    <alpha
        android:duration="2000"
        android:fromAlpha="0.1"
        android:toAlpha="1.0" />
</set>


fade_out.xml 
 

XML




<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    android:interpolator="@android:anim/linear_interpolator">
    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />
</set>


Android Fade In/Out in Kotlin

In Android Animations are the visuals that are added to make the user interface more interactive, clear and good looking. Fade In and Fade out animations are used to modify the appearance of any view over a set interval of time so that user can be notified about the changes that are occurring in our application. 
In this article we will be discussing how to create a Fade In/Out animation in Kotlin .

 

XML Attributes Description
android:duration It is used to specify the duration of animation
android:fromAlpha It is the starting alpha value for the animation, 
where 1.0 means fully opaque and 0.0 means fully transparent
android:toAlpha It is the ending alpha value
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:
 

Similar Reads

Modify activity_main.xml file

...

Add anim folder

...

Modify MainActivity.kt file

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 fade_in. Similarly, also create fade_out.xml and paste the following code.fade_in.xml...

AndroidManifest.xml file

...

Run as emulator:

...