Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
In Android, you can use animations to achieve a fading effect on a TextView
. The most common way to create a fade-in or fade-out animation is by using XML animation resources. Here's how you can add a fading animation to a TextView
:
Create the Animation Resources:
Fade In Animation (res/anim/fade_in.xml
):
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />
Fade Out Animation (res/anim/fade_out.xml
):
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" />
Apply the Animation in Your Activity or Fragment:
TextView myTextView = findViewById(R.id.my_text_view); // For Fade In Animation fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in); myTextView.startAnimation(fadeIn); // For Fade Out Animation fadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out); myTextView.startAnimation(fadeOut);
Optionally, Add Animation Listeners:
If you want to execute some code after the animation completes or at other stages of the animation, you can add an AnimationListener
:
fadeIn.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { // Animation started } @Override public void onAnimationEnd(Animation animation) { // Animation ended } @Override public void onAnimationRepeat(Animation animation) { // Animation repeated } });
That's it! You've now added a fading animation to your TextView
. You can adjust the duration
attribute in the XML files to make the animation faster or slower.
Implementing fade-in and fade-out animation for TextView:
val textView = findViewById<TextView>(R.id.textView) // Fade In Animation val fadeIn = ObjectAnimator.ofFloat(textView, "alpha", 0f, 1f) fadeIn.duration = 1000 // Fade Out Animation val fadeOut = ObjectAnimator.ofFloat(textView, "alpha", 1f, 0f) fadeOut.duration = 1000 // Play animations val animatorSet = AnimatorSet() animatorSet.playSequentially(fadeIn, fadeOut) animatorSet.start()
Using XML animation resources for fading TextView in Android:
Description: Define fade-in and fade-out animations in XML resources for cleaner and reusable code.
Code:
<!-- res/animator/fade_in.xml --> <set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:propertyName="alpha" android:valueFrom="0" android:valueTo="1" android:duration="1000" /> </set> <!-- res/animator/fade_out.xml --> <set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:propertyName="alpha" android:valueFrom="1" android:valueTo="0" android:duration="1000" /> </set>
// Load animations from XML val fadeIn = AnimatorInflater.loadAnimator(this, R.animator.fade_in) as Animator val fadeOut = AnimatorInflater.loadAnimator(this, R.animator.fade_out) as Animator // Play animations val animatorSet = AnimatorSet() animatorSet.playSequentially(fadeIn, fadeOut) animatorSet.start()
Customizing duration and interpolators for fade animation:
<!-- Customize duration and interpolator in XML --> <!-- res/animator/fade_in.xml --> <set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:propertyName="alpha" android:valueFrom="0" android:valueTo="1" android:duration="500" android:interpolator="@android:anim/accelerate_interpolator" /> </set>
Handling multiple TextViews with fading animation:
val textView1 = findViewById<TextView>(R.id.textView1) val textView2 = findViewById<TextView>(R.id.textView2) // Fade In Animation val fadeIn = ObjectAnimator.ofFloat(textView1, "alpha", 0f, 1f) fadeIn.duration = 1000 // Fade Out Animation val fadeOut = ObjectAnimator.ofFloat(textView2, "alpha", 1f, 0f) fadeOut.duration = 1000 // Play animations val animatorSet = AnimatorSet() animatorSet.playTogether(fadeIn, fadeOut) animatorSet.start()
Adding delay to fade-in or fade-out in Android TextView:
val textView = findViewById<TextView>(R.id.textView) // Fade In Animation with delay val fadeIn = ObjectAnimator.ofFloat(textView, "alpha", 0f, 1f) fadeIn.duration = 1000 fadeIn.startDelay = 500 // Play animation fadeIn.start()
Sequential fading animation for multiple TextViews:
val textView1 = findViewById<TextView>(R.id.textView1) val textView2 = findViewById<TextView>(R.id.textView2) // Fade In Animation val fadeIn1 = ObjectAnimator.ofFloat(textView1, "alpha", 0f, 1f) fadeIn1.duration = 1000 // Fade Out Animation val fadeOut1 = ObjectAnimator.ofFloat(textView1, "alpha", 1f, 0f) fadeOut1.duration = 1000 // Fade In Animation val fadeIn2 = ObjectAnimator.ofFloat(textView2, "alpha", 0f, 1f) fadeIn2.duration = 1000 // Fade Out Animation val fadeOut2 = ObjectAnimator.ofFloat(textView2, "alpha", 1f, 0f) fadeOut2.duration = 1000 // Play animations sequentially val animatorSet = AnimatorSet() animatorSet.playSequentially(fadeIn1, fadeOut1, fadeIn2, fadeOut2) animatorSet.start()
Animating text changes with a fading effect in Android:
val textView = findViewById<TextView>(R.id.textView) // Fade Out Animation val fadeOut = ObjectAnimator.ofFloat(textView, "alpha", 1f, 0f) fadeOut.duration = 500 // Set new text and start fade-in animation fadeOut.addListener(object : AnimatorListenerAdapter() { override fun onAnimationEnd(animation: Animator?) { textView.text = "New Text" val fadeIn = ObjectAnimator.ofFloat(textView, "alpha", 0f, 1f) fadeIn.duration = 500 fadeIn.start() } }) // Start fade-out animation fadeOut.start()
Creating reusable fade animation functions for TextView:
fun createFadeInAnimation(view: View, duration: Long, startDelay: Long = 0): ObjectAnimator { val fadeIn = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f) fadeIn.duration = duration fadeIn.startDelay = startDelay return fadeIn } fun createFadeOutAnimation(view: View, duration: Long, startDelay: Long = 0): ObjectAnimator { val fadeOut = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f) fadeOut.duration = duration fadeOut.startDelay = startDelay return fadeOut } // Usage val fadeIn = createFadeInAnimation(textView, 1000, 500) val fadeOut = createFadeOutAnimation(textView, 1000)