Samik Bandyopadhyay
Samik Bandyopadhyay

Reputation: 868

How to slide animate and make a view visible from right to left in Android

I want to make a view visible with slide animation effect from right to left and vise versa. I've successfully been able to hide a view with left to right slide animation but unable to achieve the other one. Following is the code snippet that I'm using :

private void showLayout(){
    if(mContainerLayout.getVisibility() == View.VISIBLE){
        mContainerLayout.animate()
                .translationX(mContainerLayout.getWidth())
                .setDuration(300)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        mContainerLayout.setVisibility(View.GONE);
                    }
                }).start();
    }
    else{
        mContainerLayout.animate()
                .translationX(0)
                .setDuration(300)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        mContainerLayout.setVisibility(View.VISIBLE);
                    }
                }).start();
    }
}

Upvotes: 2

Views: 2916

Answers (2)

Drilon Blakqori
Drilon Blakqori

Reputation: 2826

You can add a fade animation instead:

private void showLayout(boolean show){
    if (show){
        mContainerLayout.animate()
                .translationX(0)
                .setDuration(300)
                .alpha(1)
                .start();
    } else {
        mContainerLayout.animate()
                .translationX(mContainerLayout.getWidth())
                .alpha(0)
                .setDuration(300)
                .start();
    }
}

Upvotes: 5

arjun
arjun

Reputation: 3574

Create a file slide_in_anim.xml inside anim folder under res folder and add this code

<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
      android:duration="200"
      android:fillAfter="true"
      android:fromXDelta="-100%p"
      android:toXDelta="0%p" />
</set>

Then use this animation as

Animation slideAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_in_up);
mContainerLayout.startAnimation(slideAnimation);

Upvotes: 0

Related Questions