0

In this alertdialog I am getting the users to input their details to which they are then stored on Firebase. How can I do it that when the user clicks the mBookingbtn that it will close the alert dialog?

I basically want to have the function of the Negative Close Button to run automatically when the data is saved to the database

Here is the code :

final AlertDialog.Builder mBuilder = new AlertDialog.Builder(ProfileActivity.this);
                    View mView = getLayoutInflater().inflate(R.layout.welcomemessage, null);
                    final EditText mPlayer1 = (EditText) mView.findViewById(R.id.namepopup);
                    final EditText mPlayer2 = (EditText) mView.findViewById(R.id.handicappopup);
                    final EditText mPlayer3 = (EditText) mView.findViewById(R.id.agepopup);
                    final EditText mPlayer4 = (EditText) mView.findViewById(R.id.genderpopup);
                    final Button mBookingbtn = (Button) mView.findViewById(R.id.savepopup);
                    mBookingbtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            String player1 = mPlayer1.getText().toString().trim();
                            String player2 = mPlayer2.getText().toString().trim();
                            String player4 = mPlayer4.getText().toString().trim();
                            String player3 = mPlayer3.getText().toString().trim();
                            if (player1.isEmpty()) {
                                mPlayer1.setError("Please enter player 1");
                                mPlayer1.requestFocus();
                                return;
                            }
                            if (player2.isEmpty()) {
                                mPlayer2.setError("Please enter player 2");
                                mPlayer2.requestFocus();
                                return;
                            }
                            if (player3.isEmpty()) {
                                mPlayer3.setError("Please enter player 2");
                                mPlayer3.requestFocus();
                                return;
                            }
                            if (player4.isEmpty()) {
                                mPlayer4.setError("Please enter player 2");
                                mPlayer4.requestFocus();
                                return;
                            }
                            String playerone = mPlayer1.getText().toString();
                            String playertwo = mPlayer2.getText().toString();
                            String playerthree = mPlayer3.getText().toString();
                            String playerfour = mPlayer4.getText().toString();
                            DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child(userid);
                            Map newPost = new HashMap();
                            newPost.put("name", playerone);
                            newPost.put("handicap", playertwo);
                            newPost.put("age", playerthree);
                            newPost.put("gender", playerfour);
                            current_user_db.setValue(newPost);
                                Toast.makeText(ProfileActivity.this, "Details Saved", Toast.LENGTH_SHORT).show();
                        }


                    });
                    mBuilder.setNeutralButton("Close ", new DialogInterface.OnClickListener() { // define the 'Cancel' button
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    mBuilder.setView(mView);
                    AlertDialog dialog = mBuilder.create();
                    dialog.show();
                }

                }
A Kumar
  • 11
  • 4
Aaron Nicholl
  • 121
  • 2
  • 9

2 Answers2

2
dialog.dismiss();

Above line is for dismissing the dialog. add it inside code of mBookingButton click block.

Vir Rajpurohit
  • 1,869
  • 1
  • 10
  • 23
1

when you click on mBookingbtn to end of the code put dialog dismiss. create dialog object on as local object of the class.

  AlertDialog dialog;

then on mBookingbtn click end of code put below code ..

       if (dialog!=null){
        dialog.dismiss();
    }