coderGirl
coderGirl

Reputation: 45

AlertDialog with EditText and Three buttons

So i have tis code and i'm trying to create a AlertDialog with an EditTet and Three buttons the positive one, the négative one and the neutral one , but it doesn't work and the application stops

        b5.setOnClickListener(new View.OnClickListener() {
        @SuppressLint("UseCompatLoadingForDrawables")
        @Override
        public void onClick(View view) {
            AlertDialog.Builder boite;
            boite = new AlertDialog.Builder(MainActivity.this);
            boite.setTitle("boite de dialogue");
            boite.setIcon(getDrawable(R.drawable.warning_shield_96px));


           final EditText input = new EditText(MainActivity.this);
            input.setInputType(InputType.TYPE_CLASS_TEXT);
            boite.setView(input);

            boite.setPositiveButton("OUI", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //whatever action
                }
            });
            boite.show();
            boite.setNegativeButton("NON", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //whatever action
                }
            });
            boite.show();
            boite.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //whatever action
                }
            });
            boite.show();
        }
    });

Upvotes: 0

Views: 1650

Answers (1)

Yamin Yazdanpanah
Yamin Yazdanpanah

Reputation: 171

There is no need to call boite.show() several times, just call it once like below :

   b5.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("UseCompatLoadingForDrawables")
            @Override
            public void onClick(View view) {
                AlertDialog.Builder boite;
                boite = new AlertDialog.Builder(MainActivity.this);
                boite.setTitle("boite de dialogue");
                boite.setIcon(getDrawable(R.drawable.warning_shield_96px));


                final EditText input = new EditText(MainActivity.this);
                input.setInputType(InputType.TYPE_CLASS_TEXT);
                boite.setView(input);

                boite.setPositiveButton("OUI", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //whatever action
                    }
                });
                boite.setNegativeButton("NON", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //whatever action
                    }
                });
                boite.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //whatever action
                    }
                });
                boite.show();
            }
        });

AlertDialog uses Builder Pattern to initialize, so you can set different methods and buttons and anything you like, then when you call alertDialog.show() it builds the object with any configs you set before that call.

Upvotes: 1

Related Questions