Animate Views when Activity starts in Android

Viewed 105

I am finishing FlashScreen and starting MainActivity using startActivity(); and I want to slide linearLayout of MainActivity from left to right slowly like an animation. But it's not happening. At first the linearLayout loads up and then it performs the animation so it's of no use. Here's my code :

slide_from_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate android:fromXDelta="-100%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="700"/>
</set>

MainAcitivity.java

Animation animFadein = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_from_left);
mLinearLayoutParent.startAnimation(animFadein);

Please help me with this. Thanks

2 Answers

if you are sliding from any side, then this View should be "slided out" in this direction when initialised (on start). consider adding proper param to XML, in your case it would be android:translationX for mLinearLayoutParent. use some very high value to be shure that this View isn't visible, as in here you can't set translation by % of view like in <translate tag. when anim starts then android:fromXDelta="-100%" line will move View to this position (exact -100% of width, as initial for animation) at very beginning of animation

To animate every activity when it opens or closes, you need to create four animation files (namely : slide_in_right, slide_in_left, slide_out_right, slide_out_left). After creating these animations, you will have to add the following code in your values/styles.xml:

<style name="android:customActivityAnimation"     parent="@android:style/Animation.Activity">
<item name="activityOpenEnterAnimation">@anim/slide_in_right</item>
<item name="activityOpenExitAnimation">@anim/slide_out_left</item> 
<item name="activityCloseEnterAnimation">@anim/slide_in_left</item> 
<item name="activityCloseExitAnimation">@anim/slide_out_right</item> 

</style>

After that in your default application theme add this style :

<style  name="android:windowAnimationStyle">@style/customActivityAnimation</style> 
Related