Search code examples
androiddialogandroid-animationobjectanimator

How to animate dialog into screen from outside


I can't seem to animate a dialog from under the screen into the 75% of the screen. I was able to do it with the old translate animator but it doesn't allow onclick events on the view, so I implemented ObjectAnimator.

The problem is that no matter what I do, the dialog animates from the center of the screen and somehow it disappears at a point.

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.toast_goal_added, null);

dialog = new Dialog(Activity.this, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(dialoglayout);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

ObjectAnimator animator = ObjectAnimator.ofFloat(dialoglayout, "y", -200f);
animator.setDuration(1000);
animator.start();
dialog.show();

This moves the dialog up, but as you see it hides under something.

enter image description here

Layout:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rl_customtoast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:clickable="true"
        android:padding="10dp"
        android:background="@drawable/custom_toast_bg" >

    <ImageView
        android:id="@+id/iv_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/icon_add_on" />
       <LinearLayout
          android:id="@+id/ll_row"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:layout_centerVertical="true"
          android:layout_toRightOf="@+id/iv_toast"
          android:layout_marginLeft="5dp"
          android:padding="2dp"  >
        <TextView
            android:id="@+id/toast_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Goal added"
            android:textColor="#FDFDFD"
            android:textSize="16sp"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

         <TextView
            android:id="@+id/toast_text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click to see your result"
            android:textColor="#E7E7E7"
            android:textSize="15sp"/>
     </LinearLayout>

   </RelativeLayout>

So Problems

  1. Dialog starts moving from center of screen

  2. Dialog disappears at a certain point


Solution

  • I made it work by ditching the dialog and using a normal layout.

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    float displayHeight = size.y;
    AnimatorSet animSet = new AnimatorSet();
    ObjectAnimator mover = ObjectAnimator.ofFloat(include_toast, "y", displayHeight, 15*displayHeight/20);
    mover.setDuration(200);
    ObjectAnimator mover2 = ObjectAnimator.ofFloat(include_toast, "y", 15*displayHeight/20, displayHeight);
    mover2.setDuration(200);
    mover2.setStartDelay(2000);
    animSet.play(mover2).after(mover);
    animSet.start();
    

    Where include_toast is a Layout that is invisible when the main layout loads and visible only during the animation.