Learn Android with Java and Kotlin
 

Android alert dialog using Kotlin

During the flow of any app we often need to prompt the user to make some decision e.g. Deleting a file , posting comment etc . Below is the most simple way to show a promt . Here we are using AlertDialog to show a single button dialog , we can display upto three buttons using AlertDialog .

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        btn_show_alert.setOnClickListener{

            val dialogBuilder=AlertDialog.Builder(this)
            dialogBuilder.setTitle("Alert")
            dialogBuilder.setMessage("Kotlin is awesome")
            dialogBuilder.setPositiveButton("True"){ dialogInterface: DialogInterface, i: Int ->
                Toast.makeText(this,"Kotlin is awesome",Toast.LENGTH_LONG).show()
            }

            dialogBuilder.create().show()
        }

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *