How to give animation to the ViewSwitcher
Solution 1
Try setting animation inside xml as
<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" >
Solution 2
In addition to this :
Take care of switcher.showNext(); or switcher.showPrevious();
If you set an animation to the switcher, both action will result in the same animation.
Solution 3
Nothing happen or you got some error? What u mean by it won't work?
Did you start animation with switcher.showNext();
or switcher.showPrevious();
Hope it will help.. Cheers ;)
Solution 4
I am giving an alternative as mentioned in the question. Using this you will achieve the same animation that ViewPager have.
Instead of showNext, you can set displayedChild to 1.
switcherView?.inAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_in_right)
switcherView?.outAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_out_left)
switcherView?.displayedChild = 1
Instead of showPrevious, you can set displayedChild to 0.
switcherView?.inAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_in_left)
switcherView?.outAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_out_right)
switcherView?.displayedChild = 0
Animation Files: R.anim.slide_in_right:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_shortAnimTime"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
R.anim.slide_out_left
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_shortAnimTime"
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
R.anim.slide_in_left
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_shortAnimTime"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
R.anim.slide_out_right
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_shortAnimTime"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>
Each time you set displayedChild, you need to set the animation. If you don't want the animation, you can simply neglect it.
PS: Kotlin code
Related videos on Youtube
Comments
-
NullPointerException over 1 year
I have created one advertisement control which consists of ViewSwitcher....
in that control i have ImageView and TextView because advertisement are of either text or images..
Now i have to give animation to the advetisements..
I have tried following
Animation inAnimation = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left); inAnimation.setDuration(1500);
Animation outAnimation = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right); outAnimation.setDuration(1500);
And i set it to the switcher as
ViewSwitcher switcher;
switcher.setInAnimation(inAnimation);
switcher.setOutAnimation(outAnimation);
but it won't work..
Please give me any other alternative.. Or if use of above code is wrong then how to use it??
-
NullPointerException about 12 yearsi have used the switcher.showNext() method.. but it wont slide in and slide out as mentioned...
-
NullPointerException almost 12 yearsis there any way to give vertical animation like in from upper side and out from lower side like this??
-
Juuso Ohtonen over 11 years@Rashmi: You can use
"@android:anim/slide_in_up"
and"@android:anim/slide_out_down"
. If you get the error message "Resource is not public", check stackoverflow.com/a/8019405/1097104. -
Aiden Strydom about 11 yearsIf you wish to rotate it - just use
android:rotation
nest your content one more layout lower and useandroid:rotation
in the opposite direction