Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep Dialog open

private void openDialog(){

    LayoutInflater inflater = LayoutInflater.from(TrueAct.this);
    View subView = inflater.inflate(R.layout.newdialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Enter New");
    builder.setView(subView);

    blEntryExistToday = true;
    builder.setPositiveButton("ADD", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (!blEntryExistToday) {
                //CLOSE DIALOG
            }
            else {
                tvM.setText("An entry for this day already exist!");
                //DO NOT CLOSE DIALOG
            }
        }
    });

    builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_LONG).show();
            //CLOSE THE DIALOG
        }
    });

    builder.show();
}

No matter whether I click ADD or CANCEL, the dialog closes. How can I keep it open if blEntryExistToday is true.

enter image description here

I would like to keep the same theme as it is right now, with the color and text.

like image 660
Si8 Avatar asked Oct 17 '25 15:10

Si8


1 Answers

You can stop setting listener while building AlertDialog and set listeners null for positive and negative buttons and handle clicks by yourself.

You can change your code this way:

private void openDialog() {
    LayoutInflater inflater = LayoutInflater.from(TrueAct.this);
    View subView = inflater.inflate(R.layout.newdialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Enter New");
    builder.setView(subView);

    blEntryExistToday = true;
    builder.setPositiveButton("ADD", null);
    builder.setNegativeButton("CANCEL", null);

    final AlertDialog alertDialog = builder.create();

    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(final DialogInterface dialog) {
            Button positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!blEntryExistToday) {
                        //CLOSE DIALOG
                        dialog.dismiss();
                    } else {
                        tvM.setText("An entry for this day already exist!");
                        //DO NOT CLOSE DIALOG
                    }
                }
            });

            Button negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
            negativeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_LONG).show();
                    //CLOSE THE DIALOG
                    dialog.dismiss();
                }
            });
        }
    });

    alertDialog.show();
}
like image 84
Mohsen Mirhoseini Avatar answered Oct 20 '25 07:10

Mohsen Mirhoseini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!