So I have the following AppCompatDialogFragment. I want my positiveButton to close the dialog except when certain condition is true. I don't know how to achieve this.
public class ColorPicker extends AppCompatDialogFragment {
private EditText editTextCode;
private ColorPickerListener listener;
/*other stuff*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.color_picker, null);
builder.setView(view);
builder.setTitle("Enter HEX code");
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String code = editTextCode.getText().toString();
if(code.length() != 6 && !code.matches("[0-9A-F]+")){
//HERE I WANT TO PREVENT THE DIALOG FROM CLOSING!!
}
listener.applyHex(code);
}
});
/* ... */
return builder.create();
}