In this tutorial am showing you how to create an input dialog in kotlin android. The dialog will have these components:
- Title
- Message/Description
- EditText for input
- Two buttons: Positive and Negative
Here is the function:
import android.app.AlertDialog
import android.content.Context
import android.widget.EditText
fun showInputDialog(context: Context, title: String, message: String, positiveButtonText: String, negativeButtonText: String,
positiveButtonClickListener: (String) -> Unit, negativeButtonClickListener: () -> Unit) {
val builder = AlertDialog.Builder(context)
builder.setTitle(title)
builder.setMessage(message)
// Set up the input field
val input = EditText(context)
builder.setView(input)
// Set up the buttons
builder.setPositiveButton(positiveButtonText) { _, _ ->
val text = input.text.toString()
positiveButtonClickListener(text)
}
builder.setNegativeButton(negativeButtonText) { _, _ ->
negativeButtonClickListener()
}
// Create and show the AlertDialog
builder.show()
}
- Function Definition:
showInputDialog
takes the following arguments:context
: The context of the activity.title
: The title of the dialog.message
: The message to display in the dialog.positiveButtonText
: The text for the positive button.negativeButtonText
: The text for the negative button.positiveButtonClickListener
: A lambda function to be executed when the positive button is clicked. It takes the input text as an argument.negativeButtonClickListener
: A lambda function to be executed when the negative button is clicked.
- AlertDialog Builder:
AlertDialog.Builder(context)
creates an AlertDialog builder.
- Setting Title and Message:
builder.setTitle(title)
sets the dialog title.builder.setMessage(message)
sets the dialog message.
- Input Field:
val input = EditText(context)
creates an EditText view.builder.setView(input)
adds the EditText to the dialog layout.
- Buttons:
builder.setPositiveButton(...)
sets the positive button:- The first argument is the button text.
- The second argument is a listener that gets called when the button is clicked. It takes the dialog and which button was clicked (unused here).
- Inside the listener:
val text = input.text.toString()
gets the text from the input field.positiveButtonClickListener(text)
calls the provided lambda function, passing the input text.
builder.setNegativeButton(...)
sets the negative button in a similar way, callingnegativeButtonClickListener()
.
- Show Dialog:
builder.show()
creates and shows the AlertDialog.
How to Use:
// In your activity or fragment
showInputDialog(this, "Input Title", "Enter your text:", "OK", "Cancel",
{ text ->
// Handle positive button click with the input text
Toast.makeText(this, "You entered: $text", Toast.LENGTH_SHORT).show()
},
{
// Handle negative button click
Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT).show()
}
)
This will display an input dialog with the title "Input Title", message "Enter your text:", and two buttons "OK" and "Cancel". When "OK" is clicked, the entered text will be displayed in a Toast. When "Cancel" is clicked, a "Canceled" Toast will be shown.