Last active
November 18, 2018 03:26
-
-
Save kingargyle/f345792106fcf7cfb84a6fc83dbbfb22 to your computer and use it in GitHub Desktop.
A DialogFragment that support Animations useful for TV development
This file contains 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
/** | |
* Dialog Fragment with hide animations | |
*/ | |
class AnimatingDialogFragment : DialogFragment() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.TvTheme_Dialog) | |
} | |
fun dismissImmediately() { | |
super.dismiss() | |
} | |
/** | |
* So the problem is that DialogFragment animations do not work. | |
* The fragment manager does not run the animations because it is not a true fragment, | |
* and it is not attached to any container. | |
* The DialogFragment itself does not execute the window animations (it hides the window immediately). | |
* So what I do is that I intercept the dismissal of the view and animate it before executing the true dismiss() | |
*/ | |
override fun dismiss() { | |
val view = view | |
if (view != null) { | |
val animation = AnimationUtils.loadAnimation(view.context, R.anim.dialog_fade_out) | |
animation.setAnimationListener(object : Animation.AnimationListener { | |
override fun onAnimationRepeat(animation: Animation?) { | |
// Unused | |
} | |
override fun onAnimationEnd(animation: Animation?) { | |
[email protected]() | |
} | |
override fun onAnimationStart(animation: Animation?) { | |
// Unused | |
} | |
}) | |
view.startAnimation(animation) | |
} else { | |
super.dismiss() | |
} | |
} | |
/** | |
* Continuing on the logic below, we need to call our dismiss() method on the back button. | |
*/ | |
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | |
val dialog = super.onCreateDialog(savedInstanceState) | |
dialog.setOnKeyListener(object : DialogInterface.OnKeyListener { | |
override fun onKey(dialog: DialogInterface, keyCode: Int, event: KeyEvent): Boolean { | |
if (keyCode == KeyEvent.KEYCODE_BACK) { | |
if (event.action == KeyEvent.ACTION_UP) { | |
dismiss() | |
} | |
return true | |
} | |
return false | |
} | |
}) | |
return dialog | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment