Day66 of #100DaysOfCode
2 min readApr 13, 2022
Hii folks 🙌
Today I will be continuing the same pathway in which we’ll implement the Dialog box.
Unit 3: Navigation
Pathway 2: Architecture componentsSource
https://developer.android.com/courses/android-basics-kotlin/course
So, we will use the MaterialAlertDialog
from the Material Design, Components library to add a dialog to our app that follows Material guidelines. Since a dialog is UI-related, the GameFragment
will be responsible for creating and showing the final score dialog.
- First, we will add a backing property to the
score
variable. InGameViewModel
, we will change thescore
variable declaration to the following.
private var _score = 0
val score: Int
get() = _score
- Now in
GameFragment
, we will add a private function calledshowFinalScoreDialog()
. To create aMaterialAlertDialog
, we will use theMaterialAlertDialogBuilder
class to build up parts of the dialog step-by-step. Call theMaterialAlertDialogBuilder
constructor passing in the content using the fragment'srequireContext()
method.
/*
* Creates and shows an AlertDialog with the final score.
*/
private fun showFinalScoreDialog() {
MaterialAlertDialogBuilder(requireContext())
}
- As the name suggests,
Context
refers to the context or the current state of an application, activity, or fragment. It contains information regarding the activity, fragment, or application. Usually it is used to get access to resources, databases, and other system services. In this step, we pass the fragment context to create the alert dialog. - Now we will add the code to set the title on the alert dialog, and use a string resource from
strings.xml
.
MaterialAlertDialogBuilder(requireContext())
.setTitle(getString(R.string.congratulations))
- Now we will set the message to show the final score, use the read-only version of the score variable (
viewModel.score
), we added earlier.
.setMessage(getString(R.string.you_scored, viewModel.score))
- Now we will add two text buttons EXIT and PLAY AGAIN using the methods
setNegativeButton()
andsetPositiveButton()
. And now we will callexitGame()
andrestartGame()
respectively from the lambdas.
.setNegativeButton(getString(R.string.exit)) { _, _ ->
exitGame()
}
.setPositiveButton(getString(R.string.play_again)) { _, _ ->
restartGame()
}
- Now at the end, we will add
show()
, which creates and then displays the alert dialog.
.show()
- Here is the complete
showFinalScoreDialog()
method:
/*
* Creates and shows an AlertDialog with the final score.
*/
private fun showFinalScoreDialog() {
MaterialAlertDialogBuilder(requireContext())
.setTitle(getString(R.string.congratulations))
.setMessage(getString(R.string.you_scored, viewModel.score))
.setCancelable(false)
.setNegativeButton(getString(R.string.exit)) { _, _ ->
exitGame()
}
.setPositiveButton(getString(R.string.play_again)) { _, _ ->
restartGame()
}
.show()
}
That is all for Day66 ✅
Thanks for reading, See you tomorrow!
If you are reading my #100Days Journey, feel free to drop by ;)