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:
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.
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.
- package com.fadeoutapplication;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.Button;
- import android.widget.TextView;
-
- public class MainActivity extends Activity implements Animation.AnimationListener {
-
- Animation fadeout;
- Button button;
- TextView textview;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textview=(TextView)findViewById(R.id.textView1);
- button=(Button)findViewById(R.id.button1);
-
- fadeout= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_out);
- fadeout.setAnimationListener(this);
-
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
-
- textview.setAnimation(fadeout);
- }
- });
- }
-
- @Override
- public void onAnimationStart(Animation animation) {
-
- }
-
- @Override
- public void onAnimationEnd(Animation animation) {
-
- }
-
- @Override
- public void onAnimationRepeat(Animation animation) {
-
- }
- }