2

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();
 }
Flama
  • 772
  • 3
  • 13
  • 39
  • Add your if-else condition inside positive button, don't call your listener outside – Rakshit Nawani Jun 11 '18 at 04:04
  • @RakshitNawani the thing is the value of code depends of editTextCode which changes its value depending of an user input. So I really need to get is value right when the positive button is clicked. And I don't think I can do calling the listener outside. – Flama Jun 11 '18 at 04:11

3 Answers3

4

You can achieve it by overriding the OnClickListener of Positive Button as follows

        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = editTextCode.getText().toString();
                if(code.length() != 6 && !code.matches("[0-9A-F]+")){
                    //Don't dismiss
                } else{
                    dialog.dismiss();
                }
            }
        });

Note:

Remember to do it after dialog.show() is called, otherwise you will end up in getting NullPointerException.

Since you are using AppCompatDialogFragment do it as follows in onResume() of your ColorPicker

final AlertDialog dialog = (AlertDialog)getDialog();
 dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = editTextCode.getText().toString();
                if(code.length() != 6 && !code.matches("[0-9A-F]+")){
                    //Don't dismiss
                } else{
                    dialog.dismiss();
                }
            }
        });
Sagar
  • 23,903
  • 4
  • 62
  • 62
0

override onResume() in AppCompatDialogFragment

@Override
public void onResume()
{
    super.onResume();    
    final AlertDialog d = (AlertDialog)getDialog();
    if(d != null)
    {
        Button positiveButton = (Button) d.getButton(Dialog.BUTTON_POSITIVE);
        positiveButton.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        //your conditions are here
                    }
                });
    }
}

and do nothing in setPositiveButtonclick inside onCreateDialog just keep blank that

source: Sogger's answer

Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65
0

Actually, Nothing needs to do much more you already getting the interface of dialog in your positive button click event you just need to put the code for dismissing in your else condition like bellow

    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!!
                        }else {
                            dialog.dismiss();
                        }
                     if(listener!=null){
                        listener.applyHex(code);}
                     }
                });
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39