Android: Switching screens in an Activity with animations (using ViewFlipper)

Posted May 26th @ 11:48 pm by Boyan Tsolov


In this post I’ll show you how to add animations when trying to switch between screens. Usually when you switch between screens it’s a direct “poof” and the new screen appears in a very un-graceful way. The SDK offers a bunch of easy-to-use animations, and I’ll show you how to use them here.

I tried doing animations on the opening and closing of activities, but I haven’t figured that out yet fully. So instead, I will show you how to use animations when switching on objects/layers inside the same activity. It will still look like you are switching screens, but all the layout data will be in one single XML. If we were to switch between activities, each activity would have had (usually) its own layout XML.

And in the post coming after this one, I’ll show you how to start this animation and switch the screen while dragging your fingers on the touch screen. Let me be a little more precise: while dragging one finger on the touch screen. I’m not sure if the OS handles multi-touch right now – or if that’s something the device has to enable – or both.

Back to the topic: how to switch between layers using animations to make it look like you are changing screens… we will be using a ViewFlipper widget in the layout XML.

1. Create a new Android project, unless you already have one

01 new project

2. Create a new Activity class that extends

02 new class

3. Create a new directory under the /res directory and call it anim

03 res directory

4. Right-click on the new directory called anim and Import all the XML files from: Android_SDK\Platform\android-1.5\samples\ApiDemos\res\anim.

These are animations created using XML. The same animations can be created in code, but these are ready for us to use in XML.

04 anim directory

5. Open res\layout\main.xml and copy/paste the following in:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

    <ViewFlipper android:id="@+id/details"


            <TextView android:id="@+id/tv_country"
            android:text="Country" >
            <Spinner android:text=""
            <Button android:text="Next"


            <TextView android:id="@+id/tv_income"
            android:text="Income" >
            <EditText android:text=""
            <Button android:text="Previous"



This is a little long, let’s inspect it more closely.

  • The outmost layer is a LinearLayout.
  • It contains only one inner layer: ViewFlipper
  • The first-level layers inside ViewFlipper will be the screens!
      • ViewFlipper contains 2 LinearLayouts. Each LinearLayout is 1 screen.
  • The first LinearLayout contains a label, a spinner (a dropdown), and a button
  • The second LinearLayout contains a label, an edit view (input box), and a button

6. Here is what Activity1.cs looks like:

package com.warriorpoint.taxman3;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.ViewFlipper;

public class Activity1 extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        // Set main.XML as the layout for this Activity

        // Add a few countries to the spinner
        Spinner spinnerCountries = (Spinner) findViewById(;
        ArrayAdapter countryArrayAdapter = new ArrayAdapter(this,
                    new String[] { "Canada", "USA" });

        // Set the listener for Button_Next, a quick and dirty way to create a listener
        Button buttonNext = (Button) findViewById(;
        buttonNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // Get the ViewFlipper from the layout
                ViewFlipper vf = (ViewFlipper) findViewById(;

                // Set an animation from res/anim: I pick push left in
                vf.setAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.push_left_in));

        // Set the listener for Button_Previous, a quick and dirty way to create a listener
        Button buttonPrevious = (Button) findViewById(;
        buttonPrevious.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // Get the ViewFlipper from the layout
                ViewFlipper vf = (ViewFlipper) findViewById(;
                // Set an animation from res/anim: I pick push left out
                vf.setAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.push_left_out));



Let’s try and decipher the code:

  • First I set the main.xml to be the layout for this Activity
  • I input Canada and USA as the values in my dropdown (the Spinner object)
  • Then I create two listeners for the two buttons that I have Button_next and Button_previous
  • Each button does the following:
      • Gets a reference to the ViewFlipper
      • Sets an animation by passing it the context of this class and an animation from a res/anim XML file
      • showNext() or showPrevious() is called – which literally flips between the LinearLayouts in the ViewFlipper widget in the main.xml either forwards or backwards

That’s it! Run it!

Look at that fancy Spinner!

05 spinner

Click Next and watch it flow!

06 animation

Disclaimer: There is a problem with the back animation when you press “Previous”. It flickers a little and doesn’t flow properly backwards. I still have to figure out why. If anyone knows why, drop me a comment below!

Next up: Removing the “Next” and “Previous” buttons and switching screens by using your finger to drag on the touch screen.

4 Trackbacks/Pingbacks

  1. Pingback: Android: Switching screens by dragging over the touch screen | Warrior Point - Latest News on SaaS & Tutorials for On-demand Software on May 29, 2009
  2. Pingback: Android GUI examples on June 3, 2010
  3. Pingback: Animaciones para cambiar entre actividades | | AndroideityAndroideity on December 17, 2011
  4. Pingback: Sliding Image in imageView Android | Android Development tutorial | Android Development tutorial on September 28, 2012


  1. stephen
    June 6, 2009 at 04:16

    Hi for the disclaimer.. try using another animation R.anim.slide_right instead of push_left_out . thanks for the tutorial it’s really great!

  2. Boyan
    June 7, 2009 at 15:56

    Thanks Stephen, I’ll try it out!

  3. StarHan
    June 29, 2009 at 03:42

    why i can’t rotate an acitivity with y axle but it works well when a view rotates? Is there any difference between an acitivity and a view when they are appled rotateanimation which rotated with y axle?
    thanks for your help!

  4. jong10
    November 5, 2009 at 22:06

    Thank you! It’s great!!

  5. shining
    November 5, 2009 at 22:08


    I have found this for a loooooooooooooong time!

    Thank you.

  6. tse
    January 26, 2010 at 01:57

    R.anim.push_left_in error

  7. tse
    January 26, 2010 at 01:57

    help me

  8. drne
    February 19, 2010 at 11:57

    In my case it just flicks, not animates, so it may be something with emulator. I have not tried it on the device.

  9. Tom
    March 3, 2010 at 15:44

    for the diclaimer, try to set 4 different animations

    next screen:
    vf.setInAnimation() … 100 -> 0 in the animation xml
    vf.setOutAnimation() … 0 -> -100 in the animation xml

    previous direction:
    vf.setInAnimation() … -100 -> 0 in the animation xml
    vf.setOutAnimation() … 0 -> 100 in the animation xml

  10. John
    April 6, 2010 at 04:54

    Hi all i am new to android .
    i want to develop calendar application in android.
    but i dont know how to start,if any one have examples related to android plz do mail me .

    Thanks and Regards

  11. Somedude
    June 6, 2010 at 06:25

    In step 6 you wrote Activity1.cs instead of!!! Come on man :P Thanks anyhow for the great tutorial.. Keep it up!

  12. Matt
    July 8, 2010 at 15:22

    LOL, I can tell you’re an old C# developer; it’s not Activity1.cs, it’s :)

  13. rosebeat
    September 19, 2010 at 05:17

    Whenever I click on Next, I get the error “Application stopped unexpectedly” and I have to force close it. Why is that? Please help me. I have tried other examples of switching between the activites and the same thing happened. Whenever i click on next it gives me runtime error.
    Anyone has any idea why is it so?

  14. and
    September 23, 2010 at 06:17

    Have u kept the following code in AndroidManifest file

    hope this should work for you

  15. and
    September 23, 2010 at 06:17

    Have u kept the following code in AndroidManifest file

    hope this should work for you

  16. and
    September 23, 2010 at 06:21

    did u kept the foll code in AndroidManifest file

    hope this should work for you

  17. Baluk
    December 6, 2010 at 11:06

    Thanks for the helpful tutorial. I want to switch between activities by clicking a button through animate. This tutorials tells about multiple screens in one activity. So can any one tell me how to work with 2 with this animate file.

    Thank You,

  18. Sticker Printing Los Angeles CA
    December 23, 2010 at 02:47

    Android is a software stack for mobile devices that includes an operating system, middleware and key applications. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.

  19. Praveen
    December 24, 2010 at 03:46

    Hello John, I’m new to android and I also tried developing a simple calendar and I could do it. please tell me ur mail id I shall send u the sorce code, u can go thro it and understand.

    Hello Everybody,
    I need to change the layout view in my calendar application from month view to week view and day view as well. Here I should do it without changing the activity. Please someone help me in doing this with single activity.


  20. Shaiful
    February 8, 2011 at 04:15

    Can I get the source code for the animations?

  21. premsagar
    February 11, 2011 at 07:43

    how can i get animation of paper folding…?
    can i get source code for that?

  22. premsagar
    February 16, 2011 at 01:03

    Dear Sir,

    I’m new coming in Graphic development. And I need to compose a animation of Paper folding in Android.

    Anyone have tutorial or sample code for my reference?

  23. Appliance Repair Memphis TN
    February 25, 2011 at 02:42

    Android is a software stack for mobile devices that includes an operating system, middleware and key applications. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform.

  24. beginner
    April 6, 2011 at 18:41

    Please provide a zipped file, that should make it less frustrating.

  25. Alex
    October 3, 2011 at 11:49

    This is the right blog for anyone who wants to find out about this topic. Great stuff, just great!

    Best regards Alex

  26. plo
    December 11, 2011 at 13:31

    my previous button is not working …force close is error

  27. Art
    March 3, 2012 at 14:09

    Can Anybody Help me????? Is it possible to drag between XML files????

  28. Luiz Verçosa
    March 22, 2012 at 17:52

    Very good! It helped me. Thanks!!!

  29. Brazilian dude
    May 23, 2012 at 18:19

    Thanks, dude… I was getting crazy with it. God bless you!

  30. Ashish
    June 5, 2012 at 07:10

    It really helps man, thanks allot for your contribution

  31. Davidson
    June 9, 2012 at 08:45

    I have got a lot problem getting my emulator to connect the connection breaks and it doe not display my project but it works fine I use windows xp please help me

  32. Olzhas
    June 26, 2012 at 01:53

    It’s possible to create animation between class or activities? If yes, how i can do it?

  33. Olzhas
    June 26, 2012 at 01:57

    It’s possible to create animation between class or activities? If yes, how i can do it?in class i use andengine, and i want switching class of andengine

  34. Sahar.b
    July 26, 2012 at 17:29

    I’m getting white screen before the slide animation..
    what should I do?


  35. Andhika
    October 15, 2012 at 03:55

    Very good tutor, thanx !!
    I’am start create new project android now..

  36. Gezim
    March 6, 2013 at 04:24

    Hi guys, thanks for this post, I’m new here and I don’t know what is anim.push_left_in on this line vf.setAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.push_left_in));
    In my class I have no “anim” and I don’t know what should contains this anim, can anyone help me?

  37. gry komputerowe w biedronce
    April 13, 2014 at 05:38

    What i don’t understood is in truth how you are now not actually a lot more smartly-liked than you might be right now. You are so intelligent. You know thus considerably when it comes to this subject, made me in my opinion believe it from numerous varied angles. Its like women and men don’t seem to be involved until it’s something to accomplish with Girl gaga! Your own stuffs excellent. Always maintain it up!

Leave a comment

Standard Login



  • black sex movies online
  • pornstar jamie lynn videos for sale
  • bikini round up online sex
  • free threesome online sex videos
  • halloween costume sex videos
  • video
  • video
  • video
  • anime ass fuck videos online
  • discount davids bridal flower girl dresses
  • teenage sleeping beauty halloween costumes
  • video
  • Sex and the city movie watch online
  • erotic lesbian short stories online
  • cheap rates for bon petite martin
  • sleeping beauty halloween costumes for teenagers
  • amateur australian woman online video choke
  • cheap mes oral sex toys
  • watch porn wars movie online free
  • free online sexy fuck videos
  • video
  • sunporno ebony thumgs
  • costume fuck videos
  • video
  • naked teens free online videos
  • video
  • girls geisha asian costume
  • video
  • sexy costumes on girls
  • hot girls in sexy costumes
  • free online asian teen sex
  • free online nondownloadable hardcore porn
  • sexy teen pirate costume
  • video
  • video
  • free online black teen porn videos
  • free porn movies online without downloadig
  • free online sex instructional videos
  • wife ordered naked
  • hot asian lesbians online
  • omg hes naked young celebrities
  • free teen sex videos online
  • teen girl fuck buys
  • free online young porn movies
  • watch free online pornstar movies
  • online lesbian anal
  • tube online video sex
  • girls gone wild free online video
  • movie character costumes for couples
  • online teen video
  • free online viewing sex movies
  • free online sexy naked girls
  • video
  • xxx sex vedios for sale
  • young as they cum video online
  • free online full length black pussy
  • freeonline teen videos
  • wholesale xxx hardcore dvds
  • video
  • petite womens golf shirts for sale
  • british porn vids free online
  • video
  • video
  • black porn videos online
  • kim kardashian full sex video online
  • japanese girls for sale
  • free online group sex videos
  • video
  • cartoon porn free online videos
  • sexy blonde girls online webcams
  • hardcore porn online
  • free pics nude girls costa rica
  • cost afterschool program la petite
  • girls fingering girls videos online
  • free online naked pictures of celebities
  • blowjob movies free online
  • free online pictures of erotic massage
  • free online porn full movies
  • brittney from dreamgirls porn
  • anal sex on megavideo online
  • free black movies online
  • free vergin online sex videos
  • free online celebrety sex videos
  • free online porn first time lesbians
  • fuck movies online free
  • young solo anal buy
  • Katie price nude pics
  • ebony lesbians free online videos
  • free online young girl porn
  • teen sex online
  • video
  • where can i order porn movies
  • free online amateur food sex videos
  • kakashis online free sex videos
  • desi porn videos online for free
  • Nude girls online
  • free asian xxx video online
  • little caprice anal on megavideo online
  • upsidedown girl online movie
  • free online fingering porn video
  • online mature hollywood movies
  • erotic stories wife salesman
  • free online porn sex videos
  • miss muffet teen girl halloween costume
  • free online erotic lesbian sex stories
  • free online porn mature black dick
  • xxx online sex stores
  • womens petite suits on sale
  • video
  • video
  • free online pussy licking
  • video
  • free shemale porn movies online
  • online videos of sex
  • free online sex stories with pictures
  • free online hardcore sexxx videos
  • free online sleeping porn vids
  • free erotic sex manga online
  • petite size halloween costumes
  • free online amateur sex tapes
  • video
  • homemade sex video wife lomgest
  • read free hardcore sex stories online
  • free online porn videos steaming live
  • shemale anime online free video
  • petite womens buy outfits
  • free online videos of sex slaves
  • download porn online video to computer
  • video
  • watch celeb sex videos online
  • video
  • discount vibrators for my pussy
  • free online teen nudism
  • wholesale black lesbian porn dvds
  • free online photos mature sexy ladies
  • video
  • massage erotic videos online
  • indian movie online naked
  • video
  • online movie babes in toyland
  • celebrity porn movies free online
  • free teen sex videos online
  • free online barchelor party porn
  • video
  • petite southwestern apparel online
  • anal complition on megavideo online
  • best mallu sex online video sites
  • buying asian type of sex toys
  • erotic lesbian love online
  • free amateur porn video online
  • oral sex and free dvds online
  • Sex scenes online
  • female free online stripping movies porn
  • video
  • concannon vineyard petite sirah price
  • blonde teen farmgirl
  • where to buy black sexlink chickens
  • online black amateur porn community
  • video
  • media player online free sex videos
  • young ethnic homebuyers
  • hbo movie asian mailorder bride bigamist
  • erotic young girls costumes
  • free ebony lesbian video online
  • Best boobs online
  • sexy girls in costumes pics real
  • bizzare sex videoss to buy
  • asian movie online streaming
  • free online homemade sex video
  • Sex incest price
  • Asian teen mgp
  • free online teen titan porn videos
  • video
  • ebony fucking videos online
  • teen blowjob video online
  • video
  • video
  • teen toys girls sale
  • time traveler wife free online movie
  • alsscan model littlecaprice pussy
  • Sex scenes online
  • video
  • free online fuck movies
  • sales petite gray pants
  • online porn video tubes
  • video
  • free online amateur porn video
  • video
  • video
  • video online free porn
  • calvin klein petite coats on sale
  • maxxcock free porn videos online
  • mature hairy german sleepimg pussy
  • sexy school girl costume canada
  • video
  • india porn video watch online
  • male porn videos online
  • online young teen models
  • black porn videos online
  • gary colemans wife shannon price pics
  • naughty teen girls in costumes
  • free indian online sex movies
  • costumed sex pictures
  • Nude pics of katie price
  • girls wholesale party dresses
  • cuckold mature black online
  • hottest girl videos online free
  • oral sex efor sale
  • wife deepthroating online video
  • anime porn free online videos
  • sue price sex pictures
  • free online porn video lesbian
  • video
  • online video of shemale sex
  • free erotic movies watch online
  • video
  • video
  • video
  • free online classic porn video
  • free hentai online sex videos
  • free online hairy pussy porn
  • final fantasy online with naked girls
  • video
  • streaming online ebony porn
  • video
  • young fuck old videos free online
  • video
  • naked costume pics porn
  • video
  • buy size petite womens jeans
  • video
  • adult asian girls iamges
  • plus size sexy farm girl costume
  • hardcore sex videos jizzonline
  • teen girls wallpaper border
  • sexy mature videos online
  • free online photos mature women sexy
  • video
  • Meet hot girls online
  • free online videos of oral sex
  • free porn online lesbians
  • free online hardcore lesbian romance novels
  • online gallery of russian teen girls
  • online mature hollywood movies
  • blowjob video online
  • vintage blonde hairy farmgirl
  • video
  • free teen girls fuckimg
  • watch ebony bbw sex porn online
  • video
  • asian black dating man online woman
  • pictures of sex online
  • college girls sexfor tallahassee sale
  • amateur porn threesome dvds for sale
  • video
  • online big dick in ass videos
  • buy porn movies and sex toys
  • hot sex videos free online
  • free online huge dildo fuck videos
  • free ebony porn movies online
  • girls in devil costumes pics
  • watch porn movies online
  • video
  • watch free erotic movie online
  • video
  • old buys young girls
  • free gamgbang anal sex
  • amateur girls videos free online
  • radioactive teens imgage gallery
  • girls geisha asian costume clearance
  • black petite womans jacket dresses sale
  • free online tight teen pussy videos
  • celeb free online video sex
  • erotic movie sales
  • free online erotic movie
  • love girls online porn
  • porn videos for sale
  • free online hardcore lesbian hentai
  • free online young teen sex videos
  • free sex mpg online videos
  • doctors orders sex videos
  • free online porn videos with chains
  • free online website ass pussy
  • dave price wife pictures
  • longest anal fucking video online
  • japanes online sex
  • video
  • video
  • online streaming sex vids x hamster
  • online videos anal plugs
  • online sex pictures
  • naked sales girl
  • japanese girls online
  • halloween mask hood black party costume
  • free fucking hardcore sex mgp movies
  • shemale online videos
  • video
  • video
  • blondie naked farmgirl
  • video
  • free online illegal porn movies
  • video
  • video
  • read hardcore porn online
  • teen online sex eduion
  • video
  • video
  • video
  • cheap girls black silver pagent dresses
  • video
  • deep throat the movie online
  • online sex moviesdownload
  • video
  • german news online topless girl
  • free online amiture sex videos
  • free online girl sex movies
  • video
  • video
  • sexy erotic costumes
  • buy black moon movie
  • free online erotic videos for couples
  • free online anime porn tube movies
  • free full length sex vids online
  • video
  • video
  • video
  • video
  • free online tiny teens videos
  • free online black anime porn
  • Katie price nude sex tape
  • video
  • cheap pussy paris
  • free ebony lesbian video online
  • free online cock orgasm denial video
  • sites to buy softcore sex movies
  • free online sex video indian
  • claudia price sex videos
  • stories short online romantic erotic sexy
  • video
  • video
  • california asian dreamgirls
  • sexy youmg couples handjob
  • video
  • cheap small unique gifts young girls
  • tight teen pussy videos online
  • sexy young elvis costume
  • Kate beckinsale naked video
  • little caprice handjob video
  • sex pics videos cheap looking secretary
  • hot lesbian sex online
  • katie price hardcore sex
  • young amateurs pics online
  • porn priceless ex girlfriend
  • free blowjob vids and pics online
  • sexy costa rica girls at beach
  • video
  • sex free japan online
  • video
  • video
  • cheap microsuede petite pants
  • Mood disorders in teens
  • free online ass gaping movies
  • anal free online porn first time
  • japan camgirl pussy
  • best online video porn site
  • watch free amateur sex videos online
  • black girls online message board
  • xxx online sex stores
  • video
  • free online simpsons porn vids
  • girls buying sex
  • free online oon sex vids
  • online video bouncing tits
  • free online shemale video sites
  • free online harcdcore sex black girls
  • free online desi sex videos
  • dreamgirls subliminal sex torrent
  • video
  • katie price black bikini
  • online secretary fuck videos
  • free online british porn videos
  • video
  • video
  • video
  • video
  • online school girls porn
  • dc comics supergirl teen costume
  • free hot shemale online videos
  • online videos butt plugsin use
  • free video of katie price naked
  • asian horror movies online
  • girls black and white sale oxfords
  • costume porn free pics
  • sex videos online paris hilton
  • watch free big tits videos online
  • throat disorders oral sex
  • video
  • facial videos online
  • free sex movies online tv
  • young teen porn online for free