Day66 of #100DaysOfCode

Kushagra Kesav
2 min readApr 13, 2022

--

#CodeTogether Day 66/100

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. In GameViewModel, we will change the score variable declaration to the following.
private var _score = 0
val score: Int
get() = _score
  • Now in GameFragment, we will add a private function called showFinalScoreDialog(). To create a MaterialAlertDialog, we will use the MaterialAlertDialogBuilder class to build up parts of the dialog step-by-step. Call the MaterialAlertDialogBuilder constructor passing in the content using the fragment's requireContext() 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() and setPositiveButton(). And now we will call exitGame() and restartGame() 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 ;)

--

--

Kushagra Kesav
Kushagra Kesav

Written by Kushagra Kesav

Developer Relations | Communities | Software Engineering | https://www.linkedin.com/in/kushagrakesav

No responses yet