Reputation: 437
I know I sound silly putting up this question but I simply am not able to show an AlertDialog box in my app developed using android studio and kotlin. Here is the code :
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
runBlocking {
showDefaultDialog(this)
}
FuelManager.instance.basePath = getString(R.string.BaseURL)
And the function :
private fun showDefaultDialog(context: Context) {
val alertDialog = AlertDialog.Builder(context)
alertDialog.apply {
//setIcon(R.drawable.ic_hello)
setTitle("Hello")
setMessage("I just wanted to greet you. I hope you are doing great!")
setPositiveButton("Positive") { _: DialogInterface?, _: Int ->
Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show()
}
setNegativeButton("Negative") { _, _ ->
Toast.makeText(context, "Negative", Toast.LENGTH_SHORT).show()
}
setNeutralButton("Neutral") { _, _ ->
Toast.makeText(context, "Neutral", Toast.LENGTH_SHORT).show()
}
}.create().show()
}
What am I missing/doing wrong? I have tried using the code without the function but that didnt work either.
(When I run the application in debug mode and set a breakpoint in the function, the breakpoint is hit, but the alertdialog doesnt show up!)
Please help.
EDIT :
I tried this (in a test app) :
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showDefaultDialog(this)
Toast.makeText(this, "Hello!!!", Toast.LENGTH_SHORT).show()
}
The dialog is shown but Toast.makeText is also shown without waiting for an action on the alertDialog.
How to make the alertDialog blocking? (Is that the problem with my earlier code?)
Upvotes: 3
Views: 5694
Reputation: 93581
You must never block on the main thread, so even if this did work, it would make your app freeze and crash with the dreaded Application Not Responding dialog box. Remove your Toast call in onCreate()
. If you want to do something after the dialog box is closed, you have to put it inside a listener that you add to the dialog builder, such as setOnDismissListener
.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showDefaultDialog(this)
}
}
private fun showDefaultDialog(context: Context) {
val alertDialog = AlertDialog.Builder(context)
alertDialog.apply {
//setIcon(R.drawable.ic_hello)
setTitle("Hello")
setMessage("I just wanted to greet you. I hope you are doing great!")
setPositiveButton("Positive") { _: DialogInterface?, _: Int ->
Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show()
}
setNegativeButton("Negative") { _, _ ->
Toast.makeText(context, "Negative", Toast.LENGTH_SHORT).show()
}
setNeutralButton("Neutral") { _, _ ->
Toast.makeText(context, "Neutral", Toast.LENGTH_SHORT).show()
}
setOnDismissListener {
Toast.makeText(context, "Hello!!!", Toast.LENGTH_SHORT).show()
}
}.create().show()
}
If you are going to need to do stuff with things that are private to the activity, then you can let this function take a callback:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showDefaultDialog(this) {
// put stuff that happens after dialog closed in this lambda
Toast.makeText(this@MainActivity, "Hello!!!", Toast.LENGTH_SHORT).show()
}
}
}
inline fun showDefaultDialog(context: Context, crossinline onDismiss: ()->Unit) {
val alertDialog = AlertDialog.Builder(context)
alertDialog.apply {
//setIcon(R.drawable.ic_hello)
setTitle("Hello")
setMessage("I just wanted to greet you. I hope you are doing great!")
setPositiveButton("Positive") { _: DialogInterface?, _: Int ->
Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show()
}
setNegativeButton("Negative") { _, _ ->
Toast.makeText(context, "Negative", Toast.LENGTH_SHORT).show()
}
setNeutralButton("Neutral") { _, _ ->
Toast.makeText(context, "Neutral", Toast.LENGTH_SHORT).show()
}
setOnDismissListener {
onDismiss()
}
}.create().show()
}
Upvotes: 3