Get value from DialogFragment [duplicate]

Assuming that you want to foward result to the calling Activity:) try this code snippet:

public class QuantityDialogFragment extends DialogFragment implements OnClickListener {

    private EditText editQuantity;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        editQuantity = new EditText(getActivity());
        editQuantity.setInputType(InputType.TYPE_CLASS_NUMBER);

        return new AlertDialog.Builder(getActivity()).setTitle(R.string.app_name).setMessage("Please Enter Quantity")
                .setPositiveButton("OK", this).setNegativeButton("CANCEL", null).setView(editQuantity).create();

    }

    @Override
    public void onClick(DialogInterface dialog, int position) {
        String value = editQuantity.getText().toString();
        Log.d("Quantity: ", value);
        MainActivity callingActivity = (MainActivity) getActivity();
        callingActivity.onUserSelectValue(value);
        dialog.dismiss();
    }
}

and on Your activity add :

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        QuantityDialogFragment dialog = new QuantityDialogFragment();
        dialog.show(getSupportFragmentManager(), "Dialog");
    }

    /**
     * callback method from QuantityDialogFragment, returning the value of user
     * input.
     * 
     * @param selectedValue
     */
    public void onUserSelectValue(String selectedValue) {
        // TODO add your implementation.
    }
}

Taking this idea a little further, I created a listener interface inside the dialog and implemented it in the main activity.

public interface OnDialogResultListener {
    public abstract void onPositiveResult(String value);
    public abstract void onNegativeResult();
}

public void setOnDialogResultListener(OnDialogResultListener listener) {
    this.onDialogResultListener = listener;
}

Call onNegativeResult() inside an overriden onCancel(DialogInterface) and onPositiveResult(String) where you want your dialog to return the value.

Note: don't forget to dismiss() your dialog after calling onPositiveResult() or the dialog window will stay opened.

Then inside your main activity you can create a listener for the dialog, like so:

QuantityDialogFragment dialog = new QuantityDialogFragment();
dialog.setOnDialogResultListener(new QuantityDialogFragment.OnDialogResultListener() {
        @Override
        public void onPositiveResult(String value) {
            //Do something...
        }

        @Override
        public void onNegativeResult() {
            //Do something...
        }
    });

This will make your dialog easier to reuse later.