How to remove view from AlertDialog Android Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to remove view from AlertDialog Android without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am making an ALert Dialog with custom EditText Field.

I made a View variable and then associated it with my custom EditText field.

requestView = inflater.inflate(R.layout.send_request,null);

Then I added that view to my AlertDialog

alert.setView(requestView);

And after that I added the onClick Method To My Button to perform the alert Dialog action..

chatRequestbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                alert.setPositiveButton("Send", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        request = requestMsg.getText().toString();

                        send();

                    }
                });
                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                    }
                });

                alert.show();

            }
        });

It worked correctly. But after pressing cancel option on alert dialog when I press the button again to perform the alert dialog option.

It crashes with the following error.

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                           at android.view.ViewGroup.addViewInner(ViewGroup.java:4417)
                                                                           at android.view.ViewGroup.addView(ViewGroup.java:4258)
                                                                           at android.view.ViewGroup.addView(ViewGroup.java:4230)
                                                                           at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:647)
                                                                           at android.support.v7.app.AlertController.setupView(AlertController.java:463)
                                                                           at android.support.v7.app.AlertController.installContent(AlertController.java:226)
                                                                           at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
                                                                           at android.app.Dialog.dispatchOnCreate(Dialog.java:395)
                                                                           at android.app.Dialog.show(Dialog.java:294)
                                                                           at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:955)
                                                                           at com.buckydroid.anonchat.User$3.onClick(User.java:86)
                                                                           at android.view.View.performClick(View.java:5637)
                                                                           at android.view.View$PerformClick.run(View.java:22433)
                                                                           at android.os.Handler.handleCallback(Handler.java:751)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6126)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

I though making the view null and adding the view again while clicking on the button will fix the issue. But same problem again and again..

Answer

Your problem is here:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(requestView);

In this case alertis not the dialog, but a builder. So every time when you’re trying to show it – it rebuilds this dialog and trying to add same view for it – requestView. Because it is cached in the builder. To fix it – move

requestView = inflater.inflate(R.layout.send_request,null);
alert.setView(requestView);

to your OnClick method where you’re showing the dialog. So it should look like this:

chatRequestbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              requestView = inflater.inflate(R.layout.send_request,null);
              alert.setView(requestView);

                alert.setPositiveButton("Send", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        request = requestMsg.getText().toString();

                        send();

                    }
                });
                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                    }
                });

                alert.show();

            }
        });
We are here to answer your question about How to remove view from AlertDialog Android - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji