Learn about TextView in this tutorial using simple isolated examples.

Example 1: Fade Text in TextView

This example project will teach you the following concepts:

  1. How to render text in a textview.
  2. How to fade text in a TextView.
  3. How to blink text in a textview.

Here is what is created:

TextView blinking animation
TextView fade animation

Step 1: Create Project

The first step is to create a Android Project in Android Studio.

Step 2: Add Dependencies

No special or third party dependencies are needed for this project.

Step 3: Create Animation

In your res folder create a folder known as anim and add the following animation code:

res/anim/blink_effect.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="600"
        android:repeatMode="reverse"
        android:repeatCount="infinite"/>
</set>

Step 4: Design Layouts

Design your layout as follows:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <TextView
        android:id="@+id/blink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is fadein animation"
        android:textSize="25sp"
        android:padding="10dp"
        android:textColor="@color/colorAccent"
        android:background="@color/gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="START ANIMATION"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginBottom="30dp"
        />

</android.support.constraint.ConstraintLayout>

Step : Write Code

In your code load animation as below:

 Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blink_effect);

The start the animation using the startAnimation() function:

                t.startAnimation(animation1);

Here is the code the MainActivity:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    //declaring variable for Button and TextView
    Button button;
    TextView t;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button);
        t = (TextView)findViewById(R.id.blink);

        //setting clicklistener to handle the event when button is clicked
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    Animation animation1 =
                            AnimationUtils.loadAnimation(getApplicationContext(),
                                    R.anim.blink_effect);
                t.startAnimation(animation1);
                }

        });

    }

}

Run

Copy the code into your project or download the code in the reference links below.

Reference

Find the reference links below:

Number Link
1. Download code
2. Follow code author