Alert dialog:- It is used to show any type of alert or warning. It stops the user to do any type of harmful activity or guides them to use the app properly.
Let’s coding…
Create a function that is related to the AlertDialog, like alertDialog().
private fun yourFuctionName(){}
First of all you have to create alert dialog builder.
val alertDialog: AlertDialog.Builder = AlertDialog.Builder(this)
- create a variable to store alert dialog builder. like alertDialog.Then AlertDialog.Builder builds an alert dialog to show on the screen.
- Builder() function takes a context, if you are in an Activity then you should use this, and if you are in any Fragment then use the activity as Context.
You can set the Title, Message and warning image of the alert dialog which will show at the front side of the alert dialog
alertDialog.setTitle("Your aler title!")//for set Title
alertDialog.setMessage("Enter your message that you want to show the user")// for Message
alertDialog.setIcon(Your icon path) // for alert icon
We are calling alertDialog variable everywhere because it has an alert builder and we can set any component like Title, Message, etc.
You can use the positive and negative button on your alert dialog. You can set the action on positive and negative button both.
alertDialog.setPositiveButton("Yes") { dialog, id ->
// set your desired action here.
}
alertDialog.setNegativeButton("Cancel") { dialog, id ->
// set your desired action here.
}
On setPositiveButton(“ ”), You can use a string to show on the positive symbol, and on setNegativeButton(“ ”), also use a string to show the user as a negative symbol. You can set actions in their Carly crases {} with dialog parameter.
After these things, you have to create an alert to view it. create a variable to store alert dialog create()
val alert = alertDialog.create()
alert.setCanceledOnTouchOutside(false)
alert.show()
Here, we are creating a variable first to contain to alert dialog create() function. setCanceledOnTouchOutside(), this function takes a parameter (Boolean), if we use true in this, the User can not dismiss the dialog by clicking outside of the alert dialog area. if we use false then the user can dismiss the dialog by clicking outside of the dialog area. Then show() function, Finally this function will show the alert dialog on the user screen.
Set positive and negative buttons color.
val negativeButton = alert.getButton(DialogInterface.BUTTON_NEGATIVE)
negativeButton.setTextColor(Color.RED)
val positiveButton = alert.getButton(DialogInterface.BUTTON_POSITIVE)
positiveButton.setTextColor(Color.GREEN)
You can set the color of positive and negative buttons. Using two-variable like negativeButton and positiveButton then get the button calling by getButton() function. In this function, you can navigate your button type negative or positive. Then call your variable and set the text color in setTextColor() function.
- And finally, use this function wherever you want to use it. It will work properly. Take reference from below.
private fun videoCallAlert() {
val alertDialog: AlertDialog.Builder = AlertDialog.Builder(this)
alertDialog.setTitle("Video Alert !")
alertDialog.setMessage("Are you sure ?\n Do you want dismiss conversation ?")
alertDialog.setIcon(R.drawable.ic_baseline_warning)
alertDialog.setPositiveButton("Yes") { dialog, id ->
val intent = Intent(this,MainActivity::class.java)
startActivity(intent)
this.finish()
}
alertDialog.setNegativeButton("Cancel") { dialog, id ->
dialog.cancel()
}
val alert = alertDialog.create()
alert.setCanceledOnTouchOutside(false)
alert.show()
val negativeButton = alert.getButton(DialogInterface.BUTTON_NEGATIVE)
negativeButton.setTextColor(Color.RED)
val positiveButton = alert.getButton(DialogInterface.BUTTON_POSITIVE)
positiveButton.setTextColor(Color.GREEN)
}
Here, I have done in my code and it is working properly.
I hope it will help someone…