Open In App
Related Articles

Android Fade In/Out in Kotlin

Improve Article
Improve
Save Article
Save
Like Article
Like

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:
 

Modify activity_main.xml file

 

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:orientation="vertical">
 
    <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="GeeksForGeeks"
            android:layout_centerInParent="true"
            android:textSize="30sp"
            android:textStyle="bold" />
 
    <Button
            android:id="@+id/fade_in"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Fade In"
            android:layout_marginTop="140dp"
            android:layout_marginLeft="100dp" />
    <Button
            android:id="@+id/fade_out"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="140dp"
            android:layout_toRightOf="@+id/fade_in"
            android:text="Fade Out"
            android:textAllCaps="false" />
 
</RelativeLayout>


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"?>
    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"?>
    android:interpolator="@android:anim/linear_interpolator">
    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />
</set>


Modify MainActivity.kt file

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

Java




package com.geeksforgeeks.myfirstKotlinapp
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.view.animation.AnimationUtils
import kotlinx.android.synthetic.main.activity_main.*
 
class MainActivity : AppCompatActivity() {
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        //setting button onClickListener
        fade_in.setOnClickListener {
        textView.visibility = View.VISIBLE
        //loading our custom made animations
        val animation = AnimationUtils.loadAnimation(this, R.anim.fade_in)
        //starting the animation
        textView.startAnimation(animation)
    }
    fade_out.setOnClickListener {
        val animation = AnimationUtils.loadAnimation(this, R.anim.fade_out)
        textView.startAnimation(animation)
        //textview will be invisible after the specified amount
        // of time elapses, here it is 1000 milliseconds
        Handler().postDelayed({
            textView.visibility = View.GONE
        }, 1000)
    }
}
}


AndroidManifest.xml file

XML




<?xml version="1.0" encoding="utf-8"?>
          package="i.apps.fadeinout">
 
    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
 
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
 
</manifest>


Run as emulator:


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Oct, 2021
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials