Sunday, June 23, 2013

Android TextSwitcher Example

TextSwitcher

A TextSwitcher is a specialized ViewSwitcher that contains only children of type TextView. A TextSwitcher is useful to animate a label on screen. Whenever setText(CharSequence) is called, TextSwitcher animates the current text out and animates the new text in.

Two types animations are required for for TextSwitcher to switch between the texts.1: In Animation: with which Text come in the Screen.2: Out Animation: with which Text goes out from the Screen.

As you can see in below image that current Text is going OUT and Next Text is Coming In

What we Need:

We need to set the in and out Animation type of TextSwitcher like...
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);

In Example,
I have a TextSwitcher and a button called "NEXT" , when user clickes on
NEXT button TextSwitcher will switch between texts . The current Text
will go OUT and next Text will come in with specified Animation.

TextSwitcher Example :

In this Example I have following layout

when user clickes on NEXT button TextSwitcher will switch between
texts . The current Text will go OUT and next Text will come in with
specified Animation.

// Declare the in and out animations and initialize them Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);

// set the animation type of textSwitcher mSwitcher.setInAnimation(in); mSwitcher.setOutAnimation(out);

// ClickListener for NEXT button // When clicked on Button TextSwitcher will switch between texts // The current Text will go OUT and next text will come in with specified animation btnNext.setOnClickListener(new View.OnClickListener() {