0

I am trying to slide a view from right to left whose visibility is GONE on click of a button and reverse on click of another button. I have tried the below solution. But it requires the view to be VISIBLE and it will slide the view from one position to another. I want to have a sliding effect as the navigation drawer does but with a view. How can I achieve that?

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

   <translate
        android:fromXDelta="0%p"
        android:toXDelta="75%p"
        android:duration="800" />
</set>

imageView = (ImageView) findViewById(R.id.img);

// Load the animation like this
animSlide = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.slide);

// Start the animation like this
imageView.startAnimation(animSlide);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
sagar suri
  • 4,351
  • 12
  • 59
  • 122
  • what is not working here? – karan May 21 '19 at 06:10
  • I need to keep my view visibility to GONE initially and when the user clicks the button I need to show the slide effect while making the view visible. Something similar to expand and collapse but this time horizontal. – sagar suri May 21 '19 at 06:12
  • With respect to **translate** also add **alpha** property to your animation will do the trick. – Jeel Vankhede May 21 '19 at 06:16

1 Answers1

1

Visibility changes should be animated via Transition API which is available in support (androix) package :

private void toggle() {
    View imageView = findViewById(R.id.imageView);
    ViewGroup parent = findViewById(R.id.parent);

    Transition transition = new Slide(Gravity.LEFT);
    transition.setDuration(600);
    transition.addTarget(R.id.imageView);

    TransitionManager.beginDelayedTransition(parent, transition);
    imageView.setVisibility(show ? View.VISIBLE : View.GONE);
}

Here is result:

enter image description here

Here is my answer with more info.

ashakirov
  • 12,112
  • 6
  • 40
  • 40