NullPointerException
NullPointerException

Reputation: 4008

How to give animation to the ViewSwitcher

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??

Upvotes: 17

Views: 13652

Answers (5)

Swathi
Swathi

Reputation: 1684

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.That's all. Happy Coding!

PS: Kotlin code

Upvotes: 3

Tobliug
Tobliug

Reputation: 3042

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.

Upvotes: 9

Nakul Sudhakar
Nakul Sudhakar

Reputation: 1594

A "switcher.showNext();" from the last layout and a "switcher.showPrevious();" from the first layout gives error. It must be similar to the stackoverflow and stackunderflow situation in a stack.so before you call "showNext()" check its not the last layout that you are currently in and also that you are not in the first layout when calling "showPrevious()". I came across this simple mistake. Sorry for making this a post, I am (a rookie) not yet authorised to comment on posts

Upvotes: 0

pbielik
pbielik

Reputation: 391

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" >

Upvotes: 34

Ewoks
Ewoks

Reputation: 12435

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 ;)

Upvotes: 2

Related Questions