Typing Animation Effect in Android
In this article, we are going to implement a very important feature related to 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 as this attracts the attention of 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 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/typingt" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentTop = "true" android:layout_centerHorizontal = "true" android:layout_margin = "15dp" android:text = "Beginner For Beginner" android:textColor = "@color/black" android:textSize = "30dp" /> < Button android:id = "@+id/typingb" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "15dp" android:background = "@color/purple_500" android:padding = "20dp" android:text = "Typing Animation" /> </ LinearLayout > |
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code.
// Here we are adding the first letter new Handler().postDelayed(new Runnable() { @Override public void run() { typingt.setText("G"); } },300); // After that we will be appending the next // letter after some time interval new Handler().postDelayed(new Runnable() { @Override public void run() { typingt.append("e"); } },400);
Below is the complete code for the MainActivity.java file.
Java
import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { TextView typingt; Button typingb; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); typingb = findViewById(R.id.typingb); typingt = findViewById(R.id.typingt); typingb.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { new Handler().postDelayed( new Runnable() { @Override public void run() { typingt.setText( "G" ); } }, 300 ); new Handler().postDelayed( new Runnable() { @Override public void run() { typingt.append( "e" ); } }, 400 ); new Handler().postDelayed( new Runnable() { @Override public void run() { typingt.append( "e" ); } }, 500 ); new Handler().postDelayed( new Runnable() { @Override public void run() { typingt.append( "k" ); } }, 600 ); new Handler().postDelayed( new Runnable() { @Override public void run() { typingt.append( "s" ); } }, 700 ); new Handler().postDelayed( new Runnable() { @Override public void run() { typingt.append( "f" ); } }, 800 ); new Handler().postDelayed( new Runnable() { @Override public void run() { typingt.append( "o" ); } }, 900 ); new Handler().postDelayed( new Runnable() { @Override public void run() { typingt.append( "r" ); } }, 1000 ); new Handler().postDelayed( new Runnable() { @Override public void run() { typingt.append( "G" ); } }, 1100 ); new Handler().postDelayed( new Runnable() { @Override public void run() { typingt.append( "e" ); } }, 1200 ); new Handler().postDelayed( new Runnable() { @Override public void run() { typingt.append( "e" ); } }, 1300 ); new Handler().postDelayed( new Runnable() { @Override public void run() { typingt.append( "k" ); } }, 1400 ); new Handler().postDelayed( new Runnable() { @Override public void run() { typingt.append( "s" ); } }, 1500 ); } }); } } |
Output: