Skip to main content

How to add Snackbar ,Alert Dialog, CustomDialog

1. Snackbar

object.setOnClickListener {view->
    Snackbar.make(view, "You have clicked image button.", Snackbar.LENGTH_LONG).show()
}


2. Alert Dialog

private fun alertDialogFunction(){
    // use the builder class for convenient dialog construction
    val builder = AlertDialog.Builder(this)
    // set title for alert dialog
    builder.setTitle("Alert")
    // set message for alert dialog
    builder.setMesssage("This is Alert Dialog. Which is used to show alert ")
    builder.setIcon(android.R.drawable.object_name)

    // performing positive action
    builder.setPositiveButton("yes"){dialogInterface, which ->
        Toast.makeText(applicationContext,"clicked yes", Toast.LENGTH_LONG).show()
        dialogInterface.dismiss()// dialog will be dismissed
        }

    // performing cancel action
    builder.setNaturalButton("cancel"){ dialogInterface, which->
        Toast.makeText(applicationContext, "clicked cancel\n operation cancel",Toast.LENGTH_LONG).show()
        dialogInterface.dismiss()
        }

    // performing negative action
    builder.setNegativeButton("NO"){dialogInterface, which->
        Toast.makeText(applicationContext, "clicked no", Toast.LENGTH_LONG)
        dialogInterface.dismiss()
        }

    // create the alert dialog
    val alertDialog: AlertDialog = builder.create()
    // set other dialog properties
    alertDialog.setCancelable(false)// will not allow user to cancel after the clikcing on remaining screen area.
    alertDialog.show() // show the dialog to UI
}


3. Custom Dialog 

private fun customDialogFunction(){
    val customDialog = Dialog(this)
    // set the screen content from a layout resource
    // The resource will be inflated, adding all top-level views to the screen.
    customDialog.setContentView(R.layout.dialog_custom) // dialog_custom.xml that you have to create by your own
    customDialog.tv_submit.setOnClickListener(View.OnClickListener{ View->

        // application context is more safer than , using this in this case
        Toast.makeText(applicationContext,"clicked submit",Toast.LENGTH_LONG).show()
        customDialog.dismiss()// for  dialog to be dissmissed
    )}
    customDialog.tv_cancel.setOnClickListener(View.OnClickListener { View ->
        Toast.makeText(applicatonContext, "clicked cancel",Toast.LENGTH_LONG).show()
        customDialog.dismiss()
    )}
    customDialog.show()
}


4. Custom Progress bar Dialog

    private fun customProgressDialogFunction(){
        val customProgressDialog = Dialog(this)
    // set the screen content form a layout resource ex dialog_custom_progress.xml
    // the resource will be inflated , adding all top-level views to the screen
    customProgressDialog.setcontentView(R.layout.dialog_custom_progress)
   
    // start the dialog and display it on screen
    customPtogressDialog.show()
    
   
}


    btnobject.setOnClickListner(){ customProgressdialog() }

Comments