1

Expected behavior:

  1. Dialog comes up from under the screen to about 80% on the bottom
  2. Dialog stays there for 3 seconds
  3. Dialog goes back under the screen

What's really happening:

  1. Dialog comes up from under the screen to about 80% on the bottom
  2. Dialog jumps to the top of the screen
  3. Dialog stays there for 3 seconds
  4. Dialog jumps back to about 80% bottom of the screen
  5. Dialog goes back under the screen

Why?

Slide up:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
      android:fromYDelta="100%p"
      android:toYDelta="75%p"
      android:fillAfter="true"
      android:duration="1000" />

Slide down:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromYDelta="75%p"
        android:toYDelta="100%p"
        android:fillAfter="true"
        android:duration="1000" />

Style:

<style name="dialog_animation">
    <item name="android:windowEnterAnimation">@anim/slide_up</item>
    <item name="android:windowExitAnimation">@anim/slide_down</item>
</style>

Code:

dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.toast_goal_added);
dialog.getWindow().getAttributes().windowAnimations = R.style.dialog_animation;
dialog.setCancelable(true);
Handler handler = null;
    handler = new Handler(); 
    handler.postDelayed(new Runnable(){ 
         public void run(){
             dialog.cancel();
             dialog.dismiss();
         }
    }, 3500);
dialog.show();
erdomester
  • 11,789
  • 32
  • 132
  • 234

2 Answers2

0

Your code does exactly describe the following:

  1. Dialog comes up from under the screen to about 80% on the bottom - The animation ends correctly
  2. Dialog jumps to the top of the screen - the animation does not affect the real position of the dialog, you just create an animation moving the dialog to the given Y coordinate, but you NEVER tell the dialog to be displaced to the proper position AFTER the animation.
  3. Dialog stays there for 3 seconds - of course, the animation has nothing to do with this
  4. Dialog jumps back to about 80% bottom of the screen - evident, you start the animation from the given Y, but you NEVER tell the dialog to be displaced to the proper position BEFORE the animation.
  5. Dialog goes back under the screen - evident.

Solution - you need to determine the real position of the dialog where it should stay after the animation. It is the EXACT SAME thing when you make a dialog with custom coordinates. But now you can easily just add the 75% of the screen.

Some code to start, copied from this answer:

Dialog dlg = <code to create custom dialog>;

Window window = dlg.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();

wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);

Here you can add the Y coordinate of the dialog in fucntion of the screen size.

miken32
  • 42,008
  • 16
  • 111
  • 154
Jani Bela
  • 1,660
  • 4
  • 27
  • 50
  • "Determine the real position of the dialog where it should stay after the animation". Is there a way to avoid that? After all, the animation does have `fillAfter` set to true. – Simas Jan 23 '15 at 17:50
  • Your updated answer still show the dialog at the top.. Setting y doesn't change a thing. – Simas Jan 23 '15 at 17:55
  • I will try it later, and add the correct code as soon as i have time for it. – Jani Bela Jan 23 '15 at 17:57
  • Guys, even if I found the answer (post later), I will not able to click on the dialog as I found out that the click event can be set for the initial position of the button, but cannot be set during or after animation. OMG – erdomester Jan 24 '15 at 07:25
0

I managed to do it with the following code but you need to take on important thing into consideration. TranslationAnimation doesn't allow onClick events, because the only the coordinates are changed but the view itself is not moved meaning the view doesn't go anywhere, it just looks like to be moving.

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

  <translate
    android:duration="200"
    android:fromYDelta="100%p"
    android:toYDelta="75%p" />

  <translate
    android:duration="200"
    android:startOffset="2500"
    android:fromYDelta="0%p"
    android:toYDelta="25%p" />
</set>
erdomester
  • 11,789
  • 32
  • 132
  • 234