Skip to content

Instantly share code, notes, and snippets.

@diegohkd
Created April 10, 2020 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegohkd/a34f18c093ecbc62bd990f1103502500 to your computer and use it in GitHub Desktop.
Save diegohkd/a34f18c093ecbc62bd990f1103502500 to your computer and use it in GitHub Desktop.
DialogFragment with slide animations. Since the animation is applied again if Activity resumes again after showing this dialog, here there's a workaround to remove enter animation after it already happened once
...
import androidx.fragment.app.DialogFragment
import android.os.Handler
...
class MyDialogFragment : DialogFragment() {
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupSlideFromTopAnimation()
// workaround to fix slide animation repeating when activity resumes
dialog?.setOnShowListener { Handler().post { setupSlideToTopAnimation() } }
}
...
}
val DialogFragment.window: Window? get() = dialog?.window
fun DialogFragment.setupSlideFromTopAnimation() {
window?.setWindowAnimations(R.style.SlideFromTopAnimation)
}
fun DialogFragment.setupSlideToTopAnimation() {
window?.setWindowAnimations(R.style.SlideToTopAnimation)
}
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="-100%"
android:toYDelta="0" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="0"
android:toYDelta="-100%" />
</set>
<style name="SlideFromTopAnimation">
<item name="android:windowEnterAnimation">@anim/slide_from_top</item>
</style>
<style name="SlideToTopAnimation">
<item name="android:windowExitAnimation">@anim/slide_to_top</item>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment