Created
April 10, 2020 14:09
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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() } } | |
} | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val DialogFragment.window: Window? get() = dialog?.window | |
fun DialogFragment.setupSlideFromTopAnimation() { | |
window?.setWindowAnimations(R.style.SlideFromTopAnimation) | |
} | |
fun DialogFragment.setupSlideToTopAnimation() { | |
window?.setWindowAnimations(R.style.SlideToTopAnimation) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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