How to create custom Alert Dialog in Android

How to create Alert Dialog in Android. In this article, we will learn How to create Alert Dialog in the Android application. In this android tutorial, let’s create a simple android application to build Custom AlertDialog to add items to ListView and Display on the home Activity.

Android Project Structure

How to Create custom Alert Dialog in Android

Design a custom alert dialog in android. In the below android application example, We will create a simple android application to Add List items using Alert Dialog and display them in the list view.

Step 1: Create a new project with Empty Activity

  • First, we create an object of all the views in the activity_main.xml file.
  • Button object is used to connect to ButtonView of XML file, we used findViewById to assign button object to ButtonView.
  • ListView object will be assigned to List in an XML file, these objects are used to make changes to view.
  • You can use this adapter to provide views for an AdapterView, Returns a view for each object in a collection of data objects you provide.
  • getArrayAdapter is a getter we will be using this getter to notify the adapter for data change whenever we add a new item to the list.
  • DialogBuild is an object of our other class which we are using to create custom dialog
  • is the id of Button view in our XML file
  • In ArrayAdapter we are passing the value where “this” is the context, followed by the style of list view and then the list of items
  • setAdapter method is used to assign adapted to List View
  • setOnClickListener method will be triggered when the user clicks on the button and the following statement will be executed
  • we are calling openDialog method, in that method, we are calling which will display the dialog in the activity.
  • When we click on the button it will display a dialog.

package com.example.alertdialog;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
    private Button btnAlert;
    private ListView listView;
    private static ArrayAdapter<String> arrayAdapter;

    public ArrayAdapter<String> getArrayAdapter() {
        return arrayAdapter;

    private DialogBuild dialogBuild = new DialogBuild();
    protected void onCreate(Bundle savedInstanceState) {
        btnAlert = findViewById(;
        listView = findViewById(;
        arrayAdapter = new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item,dialogBuild.getList());
        btnAlert.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

    public void openDialog(){,"Dialog");


  • LinearLayout is the main container that holds all the views.
  • Linear Layout aligns all children in one direction.
  • The orientation tag sets the orientation of the view vertical or horizontal.
  • Here, Button is our button view which will display the button on the activity.
  • layout_gravity tag sets the position of the view in the parent view.
  • gravity tag used to set the position of the child view.
  • id tag gives the view a unique id.
  • text tag will set the text of the button.
  • ListView will display a list inside the parent activity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
        android:text="Add data"/>

Step 2:Create new class

Create a new class, Here, we will create a custom Dialog box.

  • Extend class with AppCompactDialogFragment
  • Override method onCreateDialog
  • AlertDialog.Builder will create a builder with which we can build an Alert Dialog
  • With the help of Builder, we can set title, message, and many other things
  • LayoutInflater shows an XML file inside another view
  • In inflater, we are passing the address of XML file “dialog_layout”
  • setView will assign the custom view to the builder
  • setNegativeButton method is used for creating the cancel button
  • DialogInterface.OnClickListener method will execute when the cancel button is pressed.
  • On the positive Button, we get the item name from edit text and then we pass it to the Array List by list.add method.
  • we create a new adapter object with the reference of array adapter from by a getter method
  • notifyDataSetChange will notify our adapter that new data has been added to the list.

package com.example.alertdialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;

public class DialogBuild extends AppCompatDialogFragment {
    private EditText editName;

    public ArrayList<String> getList() {
        return list;

    private ArrayList<String> list = new ArrayList<>();
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_layout,null);

        editName =view.findViewById(;
        builder.setView(view).setTitle("Add Items").setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

        }).setPositiveButton("Add", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            String name =editName.getText().toString();
                MainActivity mainActivity = new MainActivity();
                ArrayAdapter<String>  stringArrayAdapter= mainActivity.getArrayAdapter();

        return builder.create();



  • Here our parent view is the relative layout.
  • padding tag is used padding to view to all sides.
  • EditText is our Editable Text view.
<?xml version="1.0" encoding="utf-8"?>
    xmlns:android="" android:layout_width="match_parent"


In this way, we have learned how to create an alert Dialog to add items to ListView and Display it to the home Activity.

Android practice tasks