Search code examples
javaandroidandroid-layoutandroid-animationandroid-relativelayout

Android RelativeLayout Animation issue


I'm basically trying to animate a RelativeLayout from the bottom of my screen (Out of user's sight) to the original position of the RelativeLayout (As coded in the xml editor) with a sliding animation. Something similar to iOS's context boxes that slide in from the bottom.

Here's my XML:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <TextView
            android:id="@+id/welcomemsg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:text="Open Panel
            android:textSize="42sp" />

      <!-- The layout that slides in from nowhere -->
      <RelativeLayout
              android:id="@+id/bottomSpinner"
              android:layout_width="fill_parent"
              android:layout_height="250dp"
              android:layout_alignParentBottom="true"
              android:background="#ffffff"
              android:visibility="visible">

      </RelativeLayout>

</RelativeLayout>

My Code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_view);

        //Start the animation
        SlideToAbove();

}

//bottomSpinner is my Slide in Panel
public void SlideToAbove() {
        Animation slide = null;
        slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, -5.0f);

        slide.setDuration(400);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        bottomSpinner.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {

                bottomSpinner.clearAnimation();

                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        bottomSpinner.getWidth(), bottomSpinner.getHeight());
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                bottomSpinner.setLayoutParams(lp);

            }
        });
    }

Good news: It works.

Bad News: Not how I want it to.

This code makes the Layout slide in to the TOP of the parent layout instead of abiding the 'alignParentBottom=true' attribute of the layout. I want it to slide in from nowhere and stop at the parent bottom

What changes should I make guys?


Solution

  • If you want to slide in from bottom you can use simple android inbuilt animation like this :

    Animation animation = AnimationUtils.loadAnimation(this,R.anim.abc_slide_in_bottom);
    yourLayout.startAnimation(animation);