handle button clicks in a DialogFragment

Do along these lines

Dialog dl = mDialogFragment.getDialog();
Button btn = dl.findViewById(R.id.btn);
btn.setOnClickListener(this);

This is the code for a Dialog I'm using (the actual GUI for the dialog is defined in the layout resource confirm_dialog.xml):

public class ConfirmDialog extends DialogFragment {

    public static String TAG = "Confirm Dialog";

    public interface ConfirmDialogCompliant {
        public void doOkConfirmClick();
        public void doCancelConfirmClick();
    }

    private ConfirmDialogCompliant caller;
    private String message;

    public ConfirmDialog(ConfirmDialogCompliant caller, String message){
        super();
        this.caller = caller;
        this.message = message;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.confirm_dialog, container, false);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        ((TextView) view.findViewById(R.id.textview_confirm)).setText(message);
        ((Button) view.findViewById(R.id.ok_confirm_button)).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // When button is clicked, call up to owning activity.
                caller.doOkConfirmClick();
            }
        });
        ((Button) view.findViewById(R.id.cancel_confirm_button)).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // When button is clicked, call up to owning activity.
                caller.doCancelConfirmClick();
            }
        });
        return view;
    }

}

The dialog is created with the following lines

confirm_dialog = new ConfirmDialog(this, message);
confirm_dialog.show(getActivity().getSupportFragmentManager(), ConfirmDialog.TAG);

The interface definition is used to assure that the caller (Fragment or Activity) implements the methods to handle the events thrown by the controller. That is, a Fragment or Activity calling this dialog must implement the given interface.
Maybe there is a better solution but this is the one I figured out. Hope it helps!


here is an example to handel a cancel button click on a dialog from the FragmentDialog class:

i used android.support.v4.app.DialogFragment;

public class MyDialogFragment extends DialogFragment { 
    public MyDialogFragment(){}     
    public static String TAG = "info Dialog";
    Button btn;   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.info_layout, container);
        getDialog().requestWindowFeature(STYLE_NO_TITLE);
        btn=(Button)view.findViewById(R.id.close_dialog_btn_info_layout);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                getDialog().dismiss();
            }
        });

        return view;
    }





}