How to add animation to DialogFragment?
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().getAttributes().windowAnimations = R.style.detailDialogAnimation;
return dialog;
}
The answer was from stackoverflow.com/a/13537234/969325 but You have to set the style at onCreateDialog
function.
Animation With DialogFragment Android
The reason was that animations on device were disabled.
Shrink/Grow layout w/ animation on DialogFragment
You can define a custom animator that updates the height property at each frame of the animation. For example:
int startHeight = slideDownView.getHeight();
// Note that you should not hardcode "300" as that will be different pixel values on
// different devices - get the value from a dimen resource or scale by the
// device density
int endHeight = startHeight - getDistanceToAnimate();
// Create a simple int animator that animates between the starting and ending height
ValueAnimator animator = ValueAnimator.ofInt(startHeight, endHeight);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
// On each frame, update the view height
int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = value;
view.setLayoutParams(layoutParams);
}
@Override
public void onAnimationEnd(Animator animation) {
// Once the animation finishes, you might have to update the view's final
// height and / or its `layout_height` attribute.
}
});
animator.setDuration(getAnimationTime());
animator.start();
Hope that helps!
Navigation Architecture Component: transition animations not working for dialog
As of version 2.2.0-alpha02
this is the limitation of Navigation component. You can view the source code of DialogFragmentNavigator
However, you can pretty easily achieve animation for DialogFragment
using the following steps:
- Create a style mentioning the enter and exit animations from
anim
folder:
<style name="MyDialogAnimation">
<item name="android:windowEnterAnimation">@anim/enter_anim</item>
<item name="android:windowExitAnimation">@anim/exit_anim</item>
</style>
- Inside the DialogFragment set the style as
windowAnimations
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
dialog?.window?.attributes?.windowAnimations = R.style.MyDialogAnimation
}
Find more here.
FragmentDialog animate an layout on dismiss
As found here https://stackoverflow.com/a/8209841/379865 the solution that works is pretty simple: get the dialog and listen for key presses.
getDialog().setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
final Animation anim =
AnimationUtils.loadAnimation(getActivity(),
R.anim.translate_to_bottom);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
dismiss();
}
});
layMain.startAnimation(anim);
return true;
}
return false;
}
});
Related Topics
How to Find Older Versions of Eclipse Adt
Android How to Display 2 Listviews in One Activity One After the Other
Navigation Drawer (Google+ VS. Youtube)
Difference Between Onstart() and Onresume()
Use Uri Builder in Android or Create Url with Variables
Gson Noclassdeffounderror After Adt and Sdk Tools Update to V17
How to Send Http Post Request and Receive Response
How to Make Alarm Manager Work When Android 6.0 in Doze Mode
How to Get All Android Contacts But Without Those Which Are on Sim
How to Set Internet Options for Android Emulator
Asynctask and Looper.Prepare() Error
Viewpager with Google Maps API V2: Mysterious Black View
How to Use the Firebase Server Timestamp to Generate Date Created
Marking Sms Messages as Read/Unread or Deleting Messages Not Working in Kitkat