Introduction

In this article, you will learn how to fade_out text on button click using animation in Android.
In this first article, you will create the project. In the XML file, you will use a TextView and Button. So what you will do is to hide the textview on button click. In a Java class file, you will create an animation object. And then you call the loadAnimation() method to load the animation in the object. Now set the animation on its animation clicklistener(). At last on button click event this object to the setAnimation method as an argument to set the animation.
Step 1
Create a project like this:
1.jpg
Step 2
Create an XML file and write this.
In the XML file you will use a TextView and Button. In this application you need to hide the TextView on button click.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity"
  10. android:background="#81a3d0">
  11. <TextView
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="@string/hello_world"
  15. android:textSize="40dp"
  16. android:id="@+id/textView1"
  17. android:layout_centerHorizontal="true"
  18. />
  19. <Button
  20. android:id="@+id/button1"
  21. android:layout_height="wrap_content"
  22. android:layout_width="wrap_content"
  23. android:text="FadeOut"
  24. android:layout_alignParentBottom="true"
  25. android:layout_centerHorizontal="true"
  26. >
  27. </Button>
  28. </RelativeLayout>
Step 3
Create an anim resource file inside the res folder. Inside the anim folder you will create an XML file named fade_out and write this:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:fillAfter="true" >
  4. <alpha
  5. android:duration="1000"
  6. android:fromAlpha="1.0"
  7. android:interpolator="@android:anim/accelerate_interpolator"
  8. android:toAlpha="0.0" />
  9. </set>
Step 4
Create a Java file and write this.
In this class file, you will create an animation object. And then you call the loadAnimation() method to load the animation into the object. Now set the animation on its animation clicklistener(). At last on button click event this object to the setAnimation method as an argument to set the animation.
  1. package com.fadeoutapplication;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.animation.Animation;
  6. import android.view.animation.AnimationUtils;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. public class MainActivity extends Activity implements Animation.AnimationListener {
  10. Animation fadeout;
  11. Button button;
  12. TextView textview;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. textview=(TextView)findViewById(R.id.textView1);
  18. button=(Button)findViewById(R.id.button1);
  19. fadeout= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_out);
  20. fadeout.setAnimationListener(this);
  21. button.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View view) {
  24. textview.setAnimation(fadeout);
  25. }
  26. });
  27. }
  28. @Override
  29. public void onAnimationStart(Animation animation) {
  30. }
  31. @Override
  32. public void onAnimationEnd(Animation animation) {
  33. }
  34. @Override
  35. public void onAnimationRepeat(Animation animation) {
  36. }
  37. }
Step 5
output
2.jpg
Step 6
When you click on the fadeout button:
3.jpg