Sandra Sandra - 19 days ago 6
Android Question

handle button clicks in a DialogFragment

I have Fragment that extends DialogFragment and I have a custom layout for it which contains two edittexts and two buttons - ok and cancel. My dialog displays just fine, using the onCreateView method for specifying the layout, but I don't know how to handle button clicks. Inside the onCreateView method, button.setOnClickListener doesn't work. This may have a simple solution, but I am stuck for several hours. I would very much appreciate an advice or example code.

P.S I don't want to use AlertDialog, because in this case when clicking on the ok button, the dialog automatically dismisses itself, and I can't do a validation on the edittext's (example: when the user presses ok button and the edittext's are empty I don't want the dialog to disappear). That is way I went with the option for creating a custom dialog and easily manage the buttons behavior.

Answer

Do along these lines

Dialog dl = mDialogFragment.getDialog();
Button btn = dl.findViewbyid(R.id.btn);
btn.setOnclicklistener(this);
Comments