Reputation: 1215
I've looked trough most of the posts here on stackoverflow, but I can't get this working. I want the positive button(register) to be disabled unless all fields are completed.
How can I achieve this?
public static class UsernameDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.ipcamaccount, null))
.setPositiveButton(R.string.action_register, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int id) {
//some other code
}
}
});
builder.setView(inflater.inflate(R.layout.ipcamaccount, null))
.setNegativeButton(R.string.action_cancel, null);
return builder.create();
}
}
Upvotes: 1
Views: 1267
Reputation: 1215
I used a different approach to get this done. Hope it saves someone the trouble. I'm using this in a RecyclerView Adapter, so you have to set context. I don't know if it's the most elegant way to do it, but it's working just fine.
Here is the code:
private void myDialog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
dialogBuilder.setView(R.layout.ipcamaccount);
dialogBuilder.setPositiveButton(R.string.action_register, null);
dialogBuilder.setNegativeButton(R.string.action_cancel, null);
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
Button positiveButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPositiveButtonClicked(alertDialog);
}
});
Button negativeButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
negativeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNegativeButtonClicked(alertDialog);
}
});
}
private void onPositiveButtonClicked(AlertDialog alertDialog) {
if(some condition) {
alertDialog.dismiss();
Toast.makeText(mContext, "Some toast", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(mContext, "Some other toast", Toast.LENGTH_LONG).show();
}
}
private void onNegativeButtonClicked(AlertDialog alertDialog) {
alertDialog.dismiss();
}
Upvotes: 1
Reputation: 2962
Button positiveButton;
@Override public void onStart() {
super.onStart();
AlertDialog d = (AlertDialog) getDialog();
if (d != null) {
positiveButton = (Button)d.getButton(Dialog.BUTTON_POSITIVE);
positiveButton.setEnabled(false);
}
}
Upvotes: 0
Reputation: 394
On the positive button listener, validate the data, if the data is valid, proceed further, if not show a toast about the error.
Upvotes: 0