How to Create Blink Effect on TextView in Android? - GeeksforGeeks

21 Apr, 2021
In this article, we are going to implement a very important feature related to TextView. Here we are adding the blink text feature on a TextView. This feature can be used to show important announcements or notifications in an App. Even we can add this feature to show important links for the user. So here we are going to learn how to implement that feature. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_margin="20dp"    android:orientation="vertical"    android:padding="20dp"    tools:context=".MainActivity">      <TextView        android:id="@+id/blinktext"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_margin="15dp"        android:gravity="center"        android:text="Blink Effect"        android:textColor="@color/black"        android:textSize="30dp" />      <Button        android:id="@+id/blinkb"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="15dp"        android:background="@color/teal_200"        android:padding="20dp"        android:text="Blink Text" />      </LinearLayout>
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.animation.ArgbEvaluator;import android.animation.ObjectAnimator;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.view.animation.Animation;import android.widget.Button;import android.widget.TextView;  import androidx.appcompat.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {      TextView blinkt;    Button blinkb;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                  blinkb = findViewById(R.id.blinkb);        blinkt = findViewById(R.id.blinktext);                  blinkb.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                                  // adding the color to be shown                ObjectAnimator animator = ObjectAnimator.ofInt(blinkt, "backgroundColor", Color.BLUE, Color.RED, Color.GREEN);                                  // duration of one color                animator.setDuration(500);                animator.setEvaluator(new ArgbEvaluator());                                  // color will be show in reverse manner                animator.setRepeatCount(Animation.REVERSE);                                  // It will be repeated up to infinite time                animator.setRepeatCount(Animation.INFINITE);                animator.start();            }        });    }}
Output:
Android-View
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Retrofit with Kotlin Coroutine in Android
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
How to Post Data to API using Retrofit in Android?
Android Listview in Java with Example
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
Arrays.sort() in Java with examples