Search code examples
androidbuttondialogandroid-edittextandroid-dialogfragment

Android DialogFragment done button clickable


I have a dialog fragment with a edit text (custom layout). I want to make the "Done" button clickable only when the edit text is not empty. When the dialog opened this should be not clickable.

This is my DialogFragment class:

public class AddNumberDialog extends DialogFragment {

private Context mContext;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mContext = activity;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View mView = inflater.inflate(R.layout.dialog_add_number, null);

    final EditText mEditText = (EditText)
            mView.findViewById(R.id.number_edit_text);

    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setView(mView)
            .setPositiveButton(R.string.dialog_done, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    String number = mEditText.getText().toString();
                    //Add number to list
                }
            })
            .setNegativeButton(R.string.dialog_cancel, null);

    return builder.create();
}

}

Solution

  • You have to use Dialog. Because Alert Dialog Buttons close the Alert Dialog on press. Without Executing the code in OnClickListener. You can Prevent AlertDialog to Dismiss. See this Answer if someone is interested.

    Override the onStart Method to Set the Done Button Clickable False.

    public class AddNumberDialog extends DialogFragment {
    
            private Context mContext;
    
            @Override
            public void onAttach(Activity activity) {
               super.onAttach(activity);
               mContext = activity;
            }
    
    
        Button button; // Your done Button
    
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            View mView = inflater.inflate(R.layout.dialog_add_number, null);
    
            final EditText mEditText = (EditText)
                    mView.findViewById(R.id.number_edit_text);
    
            mEditText.addTextChangedListener(new TextWatcher(){
                  public void afterTextChanged(Editable s) {
                        if(s.length()>0){
                              button.setEnabled(true);
                        }else{
                              button.setEnabled(false);
                        }
                  }
                  public void beforeTextChanged(CharSequence s, int start, int count, int after){}
                  public void onTextChanged(CharSequence s, int start, int before, int count){}
            }); 
    
    
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setView(mView)
                    .setPositiveButton(R.string.dialog_done, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            // Do Nothing here... We Will be getting the Button Click Listener in Onstart
                        }
                    })
                    .setNegativeButton(R.string.dialog_cancel, null);
    
            return builder.create();
        }
    
        @Override
        public void onStart()
        {
            super.onStart();    //super.onStart() is where dialog.show() is actually called on the underlying dialog, so we have to do it after this point
    
            AlertDialog d = (AlertDialog)getDialog();
            if(d != null)
            {            
                button= (Button) d.getButton(Dialog.BUTTON_NEGATIVE);
                button.setOnClickListener(new View.OnClickListener()
                        {
                            @Override
                            public void onClick(View v)
                            {
    
                                String number = mEditText.getText().toString();
                                //Add number to list
    
                                Boolean wantToCloseDialog = false;
                                //Do stuff, possibly set wantToCloseDialog to true then...
                                if(wantToCloseDialog)
                                    dismiss();
                                //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
                            }
                        });
            }
        }
    }