Reputation: 2726
I have alert dialog with EditText on it. If user press positive button and EditText is empty the alert dialog is closed. Now I want to disable closing if user leaves Edit Text empty. How to do that?
This is code for alert dialog:
@Override
public void onClick(View v) {
AlertDialog.Builder wndInput = new AlertDialog.Builder(
MainActivity.this);
final EditText txtEditScraps = new EditText(MainActivity.this);
txtEditScraps.setInputType(InputType.TYPE_CLASS_PHONE);
wndInput.setTitle("Number of scraps:");
wndInput.setCancelable(false);
wndInput.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
wndInput.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
wndInput.setView(txtEditScraps);
wndInput.create().show();
}
Upvotes: 3
Views: 4888
Reputation: 12932
First of all do small changes on last 2 lines of your code..
wndInput.setView(txtEditScraps);
final AlertDialog alertDialog = wndInput.create();
alertDialog.show();
Means you just have reference the alert dialog.. and then add the following code.
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Boolean wantToCloseDialog = (txtEditScraps.getText().toString().trim().isEmpty());
// if EditText is empty disable closing on possitive button
if (!wantToCloseDialog)
alertDialog.dismiss();
}
});
Upvotes: 6
Reputation: 571
@Override
public void onClick(View v) {
AlertDialog.Builder wndInput = new AlertDialog.Builder(
MainActivity.this);
final EditText txtEditScraps = new EditText(MainActivity.this);
txtEditScraps.setInputType(InputType.TYPE_CLASS_PHONE);
wndInput.setTitle("Number of scraps:");
wndInput.setCancelable(false);
wndInput.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
wndInput.dismiss();
}
});
wndInput.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(txtEditScraps.getText().toString().trim()=="") {
txtEditScraps.requestFocus(true);
}
else {
wndInput.dismiss();
}
}
});
wndInput.setView(txtEditScraps);
wndInput.create().show();
}
Upvotes: 0
Reputation: 2825
You need to get the text of EditText
, if length of EditText
value is null then disable your setPositiveButton
event.
You can do something like this.
@Override
public void onClick(View v) {
AlertDialog.Builder wndInput = new AlertDialog.Builder(
MainActivity.this);
final EditText txtEditScraps = new EditText(MainActivity.this);
txtEditScraps.setInputType(InputType.TYPE_CLASS_PHONE);
wndInput.setTitle("Number of scraps:");
wndInput.setCancelable(false);
wndInput.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
wndInput.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String _edtValue = txtEditScraps.getText().toString().trim();
if (_edtValue.length()==0&&_edtValue==null&&_edtValue.equalsIgnoreCase("")) {
//do nothing.
} else {
// dismiss your alert.
}
}
});
wndInput.setView(txtEditScraps);
wndInput.create().show();
}
Upvotes: 1
Reputation: 3831
Try like this
final AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("My dialog")
.setPositiveButton(android.R.string.ok, null)
.setNegativeButton(android.R.string.cancel, null)
.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Now dialog will not close", Toast.LENGTH_LONG).show();
//alertDialog.dismiss();
}
});
}
});
alertDialog.show();
Upvotes: 2
Reputation: 1704
you can check a conditon inside onClick() method like this:
wndInput.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String etContent=txtEditScraps.getText();
if(!etContent.isEmpty()){
wndInput.dismiss();
}
else{// do nothing or give editText is empty}
}
});
Upvotes: 1