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 by Step Implementation
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    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:

