Animation in Android Both On TextView and Button

Jyotishgher Astrology
By -
2 minute read
0

Animating Text and Buttons in Android

Animations can significantly enhance the user experience in Android apps, adding a touch of dynamism and polish. This article demonstrates how to animate a TextView and a Button using simple alpha animations, creating a fade-in/fade-out effect.

Animation in Android Both On TextView and Button

Animating a TextView:

TextView tempno = (TextView) findViewById(R.id.mobile_number);

Animation anim = new AlphaAnimation(0.0f, 1.0f);

anim.setDuration(3000); // Duration of the animation in milliseconds

anim.setStartOffset(70); // Delay before the animation starts

anim.setRepeatMode(Animation.REVERSE); // Makes the animation go back and forth

anim.setRepeatCount(Animation.INFINITE); // Repeats the animation infinitely

tempno.startAnimation(anim);


Explanation:

  1. We first get a reference to the TextView using its ID (R.id.mobile_number).
  2. An AlphaAnimation is created, going from an alpha (opacity) of 0.0 (fully transparent) to 1.0 (fully opaque). This creates the fade-in effect.
  3. setDuration() sets the duration of the animation in milliseconds (3000 milliseconds = 3 seconds in this example).
  4. setStartOffset() introduces a small delay (70 milliseconds) before the animation begins.
  5. setRepeatMode(Animation.REVERSE) makes the animation reverse direction after it completes, creating the fade-out effect.
  6. setRepeatCount(Animation.INFINITE) makes the animation loop continuously.
  7. Finally, tempno.startAnimation(anim) applies the animation to the TextView.

Animating a Button:

Button logout = (Button) findViewById(R.id.more);

Animation mAnimation = new AlphaAnimation(1, 0); // Fade out

mAnimation.setDuration(400); // Shorter duration

mAnimation.setInterpolator(new LinearInterpolator()); // Consistent speed

mAnimation.setRepeatCount(Animation.INFINITE);

mAnimation.setRepeatMode(Animation.REVERSE);

logout.startAnimation(mAnimation);

logout.setOnClickListener(new OnClickListener() {

    @Override

    public void onClick(View arg0) {

        Intent out = new Intent(getApplicationContext(), nanoakhi.rcfinternal.staffcorner.Login.class);

        arg0.clearAnimation(); // Stop the animation when the button is clicked

        startActivity(out);

    }

}); 

Explanation:

  1. This code animates a Button with the ID R.id.more (presumably a logout button).
  2. The AlphaAnimation is similar to the TextView example, but it fades the button out (from 1.0 to 0.0).
  3. The duration is shorter (400 milliseconds).
  4. A LinearInterpolator is used to ensure the animation proceeds at a constant speed.
  5. The key difference is in the OnClickListener. When the button is clicked:
    • arg0.clearAnimation() is crucial. This stops the animation before starting the new Activity. Without this, the animation might continue to run over the new screen.
    • The Intent starts the Login Activity.

Conclusion:

These examples demonstrate how easily you can add simple yet effective animations to TextViews and Buttons in Android. Alpha animations are a great way to create subtle visual effects that can improve the user experience. Remember to stop any ongoing animations before starting new Activities or performing other actions that change the view hierarchy to prevent unexpected behavior. Experiment with different animation types, durations, interpolators, and repeat modes to create a variety of engaging effects in your Android applications. This simple technique can significantly enhance the visual appeal and user engagement of your app. 

Tags:

Post a Comment

0Comments

Post a Comment (0)