How to add fading TextView animation in Android

23 Feb, 2021
TextView is the basic building block of user interface components. It is used to set the text and display it to the user. It is a very basic component and used a lot.
A Fading TextView is a TextView that changes its content automatically every few seconds. If we want to design a beautiful interface than we can use Fading TextView.
Approach:
Add this to your root build.gradle file (not your module build.gradle file):allprojects {    repositories {           jcenter()    }}Add the support Library in your module’s build.gradle file and add dependency in the dependencies section.dependencies {    implementation 'com.tomer:fadingtextview:2.5'}Now add the following code in the activity_main.xml file.activity_main.xmlactivity_main.xml<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout       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">      <com.tomer.fadingtextview.FadingTextView        android:id="@+id/fadingTextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="22sp"        android:textColor="#2AC308"        android:textStyle="bold"        app:timeout="500"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf="parent" />  </androidx.constraintlayout.widget.ConstraintLayout>Now add the following code in the MainActivity.java file.MainActivity.javaMainActivity.javapackage org.geeksforgeeks.gfgFadingTextView;  import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import com.tomer.fadingtextview.FadingTextView;  public class MainActivity extends AppCompatActivity {      FadingTextView fadingTextView;    String[] text        = { "GeeksForGeeks", "A",            "Computer", "Science", "Portal",            "For", "Geeks" };      @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          fadingTextView            = findViewById(R.id.fadingTextView);        fadingTextView.setTexts(text);    }}
Add this to your root build.gradle file (not your module build.gradle file):allprojects {    repositories {           jcenter()    }}
allprojects {    repositories {           jcenter()    }}
Add the support Library in your module’s build.gradle file and add dependency in the dependencies section.dependencies {    implementation 'com.tomer:fadingtextview:2.5'}
dependencies {    implementation 'com.tomer:fadingtextview:2.5'}
Now add the following code in the activity_main.xml file.activity_main.xmlactivity_main.xml<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout       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">      <com.tomer.fadingtextview.FadingTextView        android:id="@+id/fadingTextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="22sp"        android:textColor="#2AC308"        android:textStyle="bold"        app:timeout="500"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf="parent" />  </androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout       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">      <com.tomer.fadingtextview.FadingTextView        android:id="@+id/fadingTextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="22sp"        android:textColor="#2AC308"        android:textStyle="bold"        app:timeout="500"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf="parent" />  </androidx.constraintlayout.widget.ConstraintLayout>
Now add the following code in the MainActivity.java file.MainActivity.javaMainActivity.javapackage org.geeksforgeeks.gfgFadingTextView;  import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import com.tomer.fadingtextview.FadingTextView;  public class MainActivity extends AppCompatActivity {      FadingTextView fadingTextView;    String[] text        = { "GeeksForGeeks", "A",            "Computer", "Science", "Portal",            "For", "Geeks" };      @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          fadingTextView            = findViewById(R.id.fadingTextView);        fadingTextView.setTexts(text);    }}
MainActivity.java
package org.geeksforgeeks.gfgFadingTextView;  import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import com.tomer.fadingtextview.FadingTextView;  public class MainActivity extends AppCompatActivity {      FadingTextView fadingTextView;    String[] text        = { "GeeksForGeeks", "A",            "Computer", "Science", "Portal",            "For", "Geeks" };      @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          fadingTextView            = findViewById(R.id.fadingTextView);        fadingTextView.setTexts(text);    }}
Output:
Android-Animation
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.