Search code examples
androidanimationdrop-down-menu

Drop-down animation


I am trying to create a drop-down animation. When the user taps on a specified button @+id/btnToHideView, I would like a view @+id/relativeLayoutControls to drop down/slide up (VISIBLE / GONE).

Here is how the layout file looks:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnToHideView"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/hide_btn"
        />

    <RelativeLayout
        android:id="@+id/relativeLayoutControls"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dp"
        android:layout_marginEnd="6dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        //I have buttons in this layoout

    </RelativeLayout>

</LinearLayout>

and this is what I have tried:

I created 2 animation files in res/anim

slide_down.xml

    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="1000"
        android:fromYDelta="0"
        android:toYDelta="100%" />
</set>

slide_up.xml

    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="1000"
        android:fromYDelta="100%"
        android:toYDelta="0" />
</set>

Then I tried handling this by doing:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    controlsHide = (RelativeLayout) findViewById(R.id.relativeLayoutControls);

    final Animation slide_down = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.slide_down);

    final Animation slide_up = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.slide_up);

    btnToHideView = (Button) findViewById(R.id.btnToHideView);

    btnToHideView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            //I just did slide_up to test if its working
            controlsHide.startAnimation(slide_up);

        }
    });

I followed this post, but when I tap on the button nothing happens. In logcat, it only prints ACTION_DOWN.


Solution

  • Please try this:

    SlideUp:

    Animation slideUp = AnimationUtils.loadAnimation(activity, R.anim.slide_up);
    view.startAnimation(slideUp);
    

    slide_up.xml

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

    or

    view.animate().translationY(0);
    

    Dropdown:

    view.animate().translationY(view.getHeight());