In some cases for the AlertDialog, there is a need to get input from the user or customize it according to our requirements. That's where custom AlertDialogs comes in handy. In this article we are going to discuss on how to customize the AlertDialogs and take user input.
Example:

Steps to Implement of Custom AlertDialog
Step 1: Create an XML file custom_layout.xml
Add the below code in custom_layout.xml. This code defines the alert dialog box dimensions and adds an edit text to it.
custom_layout.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:paddingLeft="20dp"
android:paddingRight="20dp">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Step 2: Add a Button in activity_main.xml
The button when clicked will show the AlertDialog box.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog" />
</LinearLayout>
Layout:
Step 3: Add custom_layout.xml file
Add custom_layout.xml in that activity in which you want to show a custom alert dialog here it is added in MainActivity.
MainActivity File:
package org.geeksforgeeks.demo;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
// Create an alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Name");
// Set the custom layout
LayoutInflater inflater = getLayoutInflater();
View customLayout = inflater.inflate(R.layout.custom_layout, null);
builder.setView(customLayout);
// Add a button
builder.setPositiveButton("OK", (dialog, which) -> {
// Send data from the AlertDialog to the Activity
EditText editText = customLayout.findViewById(R.id.editText);
Toast.makeText(MainActivity.this, editText.getText().toString(), Toast.LENGTH_SHORT).show();
});
// Create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
});
}
}
package org.geeksforgeeks.demo
import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button: Button = findViewById(R.id.button)
button.setOnClickListener {
// Create an alert builder
val builder = AlertDialog.Builder(this)
builder.setTitle("Name")
// set the custom layout
val customLayout: View = layoutInflater.inflate(R.layout.custom_layout, null)
builder.setView(customLayout)
// add a button
builder.setPositiveButton("OK") { dialog: DialogInterface?, which: Int ->
// send data from the AlertDialog to the Activity
val editText: EditText = customLayout.findViewById(R.id.editText)
Toast.makeText(this, editText.text.toString(), Toast.LENGTH_SHORT).show()
}
// create and show the alert dialog
val dialog = builder.create()
dialog.show()
}
}
}