Skip to content
Related Articles

Related Articles

How to create a custom AlertDialog in Android

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 01 Jun, 2020
Improve Article
Save Article

Sometimes in AlertDialog, there is need to get input from the user or customize according to our requirements. So we create custom AlertDialogs. This post will show how to customize the AlertDialogs and take the input from it.

Below is the step by step implementation of the above approach:

  • Step 1: Create a XML file: custom_layout.xml. Add the below code in custom_layout.xml. This code defines the alertdialog box dimensions and add a edittext in it.

    <?xml version="1.0" encoding="utf-8"?>

  • Step 2: Add a button in activity_main.xml. The button when clicked will show the AlertDialog box.

    <?xml version="1.0" encoding="utf-8"?>
            android:text="Show Dialog"

  • Step 3:

    Add custom_layout.xml in that activity in which you want to show custom alert dialog here it is added in

    public class MainActivity
        extends AppCompatActivity {
        protected void onCreate(
            Bundle savedInstanceState)
        public void showAlertDialogButtonClicked(View view)
            // Create an alert builder
            AlertDialog.Builder builder
                = new AlertDialog.Builder(this);
            // set the custom layout
            final View customLayout
                = getLayoutInflater()
            // add a button
                    new DialogInterface.OnClickListener() {
                        public void onClick(
                            DialogInterface dialog,
                            int which)
                            // send data from the
                            // AlertDialog to the Activity
                            EditText editText
                                = customLayout
            // create and show
            // the alert dialog
            AlertDialog dialog
                = builder.create();
        // Do something with the data
        // coming from the AlertDialog
        private void sendDialogDataToActivity(String data)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!