1

E.g. I have similar code to this post Show AlertDialog if some condition is met

if (condition) {

                AlertDialog.Builder(requireActivity())
                .setTitle("Internet")
                .setMessage("Internet ON")
                .setPositiveButton("ok!",null)
                .create()
            } else {

                AlertDialog.Builder(requireActivity())
                .setTitle("Internet")
                .setMessage("Internet OFF")
                .setPositiveButton("ok!",null)
                .create()
            }

But can I do the check for conditions met inside the message for dialog? So that I do not rewrite same code twice, but with different message

1 Answers1

2

If you want to use ternary operator like syntax. Kotlin provides kinda the same logic, just a bit different syntax.

Here's an example.

AlertDialog.Builder(requireActivity())
                .setTitle("Internet")
                .setMessage(if (condition) "Internet ON" else "Internet OFF")
                .setPositiveButton("ok!",null)
                .create()

Read more here: https://stackoverflow.com/a/46843369/3010171

Christian Moen
  • 1,253
  • 2
  • 17
  • 31