Animate an AlertDialog's entrance and exit

半城伤御伤魂 提交于 2019-12-10 16:19:50

问题


I have to slide in an AlertDialog when it enters and slide it out when it is dismissed, but it is not animating.

So how do I get the animation to work ?

Here is what I have,

public class SlideDialogFragment extends DialogFragment {
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
              return  new AlertDialog.Builder(new ContextThemeWrapper(getActivity(),R.style.SlidingDialog))
                      .setTitle("Sliding dialog")
                      .create()
     }

themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="SlidingDialog" parent="@android:style/Theme.DeviceDefault.Dialog">
        <item name="android:windowAnimationStyle">@style/SlidingDialogAnimation</item>
    </style>
    <style name="SlidingDialogAnimation">
        <item name="android:windowEnterAnimation">@android:anim/slide_in_left</item>
        <item name="android:windowExitAnimation">@android:anim/slide_out_right</item>
    </style>
</resources>

I have referred too many resources and there doesn't seem to be a single proper way to do this that works for me, may be I am missing something

I am using

  • Android ICS
  • App is built for API 15+

Here are some related resources that I couldn't get the answer from

  • Animate a dialog fragment on dismiss
  • https://groups.google.com/d/msg/android-developers/0oCWqQC4Pww/CmUM7iNHUggJ
  • https://groups.google.com/d/msg/android-developers/a2pUV0Sigf4/WiJNg_vMQWwJ
  • https://stackoverflow.com/a/3870997/492561

回答1:


Here is the working code using the reference and code from the above.

// Declare a Builder.
AlertDialog.Builder builder = new AlertDialog.Builder(context);

// You can even inflate a layout to the AlertDialog.Builder, if looking for to create a custom one.
// Add and fill all required builder methods, as per your need.


// Now create object of AlertDialog from the Builder.
final AlertDialog dialog = builder.create();

// Let's start with animation work. We just need to create a style and use it here as follow.
if (dialog.getWindow() != null)
    dialog.getWindow().getAttributes().windowAnimations = R.style.SlidingDialogAnimation;

dialog.show();

Regarding Style, I used the same style as used in question (in styles.xml).

<style name="SlidingDialogAnimation">
        <item name="android:windowEnterAnimation">@android:anim/slide_in_left</item>
        <item name="android:windowExitAnimation">@android:anim/slide_out_right</item>
</style>

But you can use any other custom files by placing animation xml file in res/anim folder.

...

Note:- If being a SO user, you are just interesting in down voting the answer without implementing or testing it, then I feel sorry for you. For any doubt or further help on it, just left me a comment below.

Thanks.



来源:https://stackoverflow.com/questions/12048886/animate-an-alertdialogs-entrance-and-exit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!