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
.
I would like to keep the same theme as it is right now, with the color and text.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With