Toast vs Snack Bar
Toast:
Snack Bar
image_btn.setOnClickListener { view ->
Snackbar.make(view, "You have clicked image button.", Snackbar.LENGTH_LONG).show()
}
alert_btn.setOnClickListener { view ->
alertDialogFunction()
}
private fun alertDialogFunction() {
val builder = AlertDialog.Builder(this)
//set title for alert dialog
builder.setTitle("Alert")
//set message for alert dialog
builder.setMessage("This is Alert Dialog. Which is used to show alerts in our app.")
builder.setIcon(android.R.drawable.ic_dialog_alert)
//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.setNeutralButton("Cancel") { dialogInterface, which ->
Toast.makeText(
applicationContext,
"clicked cancel\n operation cancel",
Toast.LENGTH_LONG
).show()
dialogInterface.dismiss() // Dialog will be dismissed
}
//performing negative action
builder.setNegativeButton("No") { dialogInterface, which ->
Toast.makeText(applicationContext, "clicked No", Toast.LENGTH_LONG).show()
dialogInterface.dismiss() // Dialog will be dismissed
}
// Create the AlertDialog
val alertDialog: AlertDialog = builder.create()
// Set other dialog properties
alertDialog.setCancelable(false) // Will not allow user to cancel after clicking on remaining screen area.
alertDialog.show() // show the dialog to UI
}
btnCustomDialog.setOnClickListener { view ->
customDialogFunction()
}
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)
customDialog.findViewById<TextView>(R.id.tv_submit).setOnClickListener {
Toast.makeText(applicationContext, "clicked submit", Toast.LENGTH_LONG).show()
customDialog.dismiss() // Dialog will be dismissed
}
customDialog.findViewById<TextView>(R.id.tv_cancel).setOnClickListener {
Toast.makeText(applicationContext, "clicked cancel", Toast.LENGTH_LONG).show()
customDialog.dismiss()
}
//Start the dialog and display it on screen.
customDialog.show()
}
dialog_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Dialog Title"
android:textColor="@android:color/black"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Custom Dialog Content Details. Which you can set your own as per requirement."
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_submit"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:foreground="?attr/selectableItemBackground"
android:gravity="center"
android:padding="10dp"
android:text="SUBMIT"
android:textColor="@android:color/holo_red_dark"/>
<TextView
android:id="@+id/tv_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:foreground="?attr/selectableItemBackground"
android:gravity="center"
android:padding="10dp"
android:text="CANCEL"
android:textColor="@android:color/black"/>
</LinearLayout>
</LinearLayout>
MainActivity.kt
btnCustomProgressDialog.setOnClickListener { view ->
customProgressDialogFunction()
}
private fun customProgressDialogFunction() {
val customProgressDialog = Dialog(this)
/*Set the screen content from a layout resource.
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.
customProgressDialog.show()
}
dialog_custom_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">
<ProgressBar
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please Wait..."
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>