Blinking a text in Android is very simple and needs very less code. Let's see how to make it!
In the activity_main.xml file you have to edit the android:text and add just these two lines under TextView tag:
android:id="@+id/textView"
android:textSize="40sp"
android:text="I AM BLINKING!"
So after making the changes, the activity_main.xml file will have these things:
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="40sp"
android:text="I AM BLINKING!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now inside the MainActivity.java, below the setContentView method, just paste this code and you are done:
TextView blinkingText = findViewById(R.id.textView);
Animation blinkAnimation = new AlphaAnimation(0.0f, 1.0f);
blinkAnimation.setDuration(1000); // duration
blinkAnimation.setStartOffset(20);
blinkAnimation.setRepeatMode(Animation.REVERSE);
blinkAnimation.setRepeatCount(Animation.INFINITE);
Understand the code:
TextView blinkingText = findViewById(R.id.textView);
→ Retrieves the TextView with ID textView from the layout and stores it in blinkingText.
Animation blinkAnimation = new AlphaAnimation(0.0f, 1.0f);
→ Creates an alpha animation that changes the opacity from fully transparent to fully opaque.
blinkAnimation.setDuration(1000);
→ Sets the duration of one animation cycle to 1000 milliseconds (1 second).
blinkAnimation.setStartOffset(20);
→ Adds a 20-millisecond delay before the animation starts.
blinkAnimation.setRepeatMode(Animation.REVERSE);
→ Makes the animation reverse back to the start after reaching the end.
blinkAnimation.setRepeatCount(Animation.INFINITE);
→ Repeats the animation endlessly.
Finally the MainActivity.java will have this complete code:
package com.prashantsj.apps.blink;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
TextView blinkingText = findViewById(R.id.textView);
Animation blinkAnimation = new AlphaAnimation(0.0f, 1.0f);
blinkAnimation.setDuration(1000); // duration
blinkAnimation.setStartOffset(20);
blinkAnimation.setRepeatMode(Animation.REVERSE);
blinkAnimation.setRepeatCount(Animation.INFINITE);
blinkingText.startAnimation(blinkAnimation);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}