3

I set the animation in the layout like this:

<ViewSwitcher
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:inAnimation="@android:anim/slide_in_left"
    android:outAnimation="@android:anim/slide_out_right" >

How can I do the same programmatically?

user
  • 86,916
  • 18
  • 197
  • 190
selva_pollachi
  • 4,147
  • 4
  • 29
  • 42

3 Answers3

10

Please read the documentation for the ViewSwitcher class, it has two methods for setting the in/out animation:

// load the two animations  
Animation animIn = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
Animation animOut = AnimationUtils.loadAnimation(context, android.R.anim.slide_out_right);
// set them on the ViewSwitcher
vs.setInAnimation(animIn);
vs.setOutAnimation(animOut);
deadfish
  • 11,996
  • 12
  • 87
  • 136
user
  • 86,916
  • 18
  • 197
  • 190
1
viewswitcher.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_in));
viewswitcher.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_out));
confucius
  • 13,127
  • 10
  • 47
  • 66
0

You can skip the step of loading the animation with AnimationUtils and instead pass the anim resource directly:

switcher.setInAnimation(this, android.R.anim.slide_in_left);
switcher.setOutAnimation(this, android.R.anim.slide_out_right);
Florian Walther
  • 6,237
  • 5
  • 46
  • 104