i have create a custom dialog where user will provide four information necessary from user , but if user click any button of alert dialog with out providing full information, the dialog is close, i want that alert not close unless the user provide full information this is Custom alert dialog
enter image description here
this is java code for custom alert dialog
final ExtraFunction ef = new ExtraFunction(mContext);
final String PhoneNumber = holder.bdContactNumber.getText().toString();
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View promptsView = layoutInflater.inflate(R.layout.dialog_contact_for_blood, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setView(promptsView);
final EditText etSubject = (EditText) promptsView.findViewById(R.id.etSubject);
etSubject.setText(holder.bdBloodgroup.getText().toString());
final EditText etNumber_Bottles = (EditText) promptsView.findViewById(R.id.etNumber_Bottles);
final EditText etRequired_At = (EditText) promptsView.findViewById(R.id.etRequired_At);
final EditText etContact_number = (EditText) promptsView.findViewById(R.id.etContact_number);
alertDialogBuilder.setCancelable(false).setPositiveButton(R.string.send_message, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// get user input and set it to result
// edit text
String Subject = etSubject.getText().toString();
String Number_Bottles = etNumber_Bottles.getText().toString();
String Required_At = etRequired_At.getText().toString();
String Contact_number = etContact_number.getText().toString();
String blood_message = ""+Subject+" Blood "+Number_Bottles+" in Quantity is urgently required at "+Required_At+". Kindly contact at "+Contact_number+". Thanks.\n" + "("+R.string.app_name+")";
if(etNumber_Bottles.length()==0 && etRequired_At.length()==0 && etContact_number.length()==0)
{
Toast.makeText(mContext, "Please Enter All Values....", Toast.LENGTH_SHORT).show();
return;
}
else
{
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(PhoneNumber, null, blood_message, null, null);
Toast.makeText(mContext, R.string.success_message, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(mContext, R.string.failed_message + PhoneNumber, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
// ef.SendSmsFunction(PhoneNumber,blood_message);
}
}).setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
i search but not clear about my problem
You did some wrong validation
if(etNumber_Bottles.length()==0 && etRequired_At.length()==0 && etContact_number.length()==0)
{
Toast.makeText(mContext, "Please Enter All Values....", Toast.LENGTH_SHORT).show();
return;
}
as this validation works only when all the field empty instead of && operator you should use OR operator and take the string value of editText dont use length value direct on edit text first type cast to string than get the length. You can take use length method or equals method for this like below.
You can use this.
if(etNumber_Bottles.getText().toString().tolength()==0 || etRequired_At.getText().toString().length()==0 || etContact_number.getText().toString().length()==0)
{
Toast.makeText(mContext, "Please Enter All Values....", Toast.LENGTH_SHORT).show();
return;
}
Or by equals method:
if(etNumber_Bottles.getText().toString().equals("") || etRequired_At.getText().toString().equals("") || etContact_number.getText().toString().equals(""))
{
Toast.makeText(mContext, "Please Enter All Values....", Toast.LENGTH_SHORT).show();
return;
}
setOnShowListener
may solve your problem:
AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setTitle("Title")
.setMessage("Messsage")
.setPositiveButton(android.R.string.ok, null)
.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(final DialogInterface dialog) {
Button buttonOk = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
if (buttonOk != null) {
buttonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Write what you want to do on button click
}
});
}
}
});
Courtesy of this .
By the way, take a look on EditText#setError
instead of yours Toast.makeText
. Looks much more pretty!
PPS I have changed my answer. You have to create AlertDialog and define setOnShowListener
and your dialog will not closed on the button click . Is it what you desire?
Bro you need to learn how to validate view look at this example - https://stacktips.com/tutorials/android/edittext-validation-in-android-example and https://www.excella.com/insights/how-do-i-validate-an-android-form and https://stackoverflow.com/a/33072633/4741746
there are some library provide easy way of validation one you can prefer is https://github.com/ragunathjawahar/android-saripaar
Best of luck
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.