Q:

How to get a string value only when 'ok' button is clicked of text entry dialog?

i have made a function taketext() which returns a string value of entered text in a text entry dialogbox. Below is the code (which is obviously incorrect). I want this to return the string only when 'ok' button is clicked. If the 'if' is removed, then it always returns null. This takeText() method is called by another method.  
private String takeText() {
    String text = null;
    LayoutInflater inflater = LayoutInflater.from(this);
    View addView = inflater.inflate(R.layout.text_tag, null);
    final DialogWrapper wrapper = new DialogWrapper(addView);

    new AlertDialog.Builder(this)
            .setTitle("Enter Text")
            .setView(addView)
            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // processAdd(wrapper);
                    // text = wrapper.getData();
                    okBClicked = true;

                }
            })
            .setNegativeButton("cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // ignore, just dismiss
                        }
                    }).show();
    if (okBClicked)
        return wrapper.getData();
    else
    //???
}

A:

Dont make your method to return string. Code it to show popup for entering string. Later, you have to get reference to your EditText defined in R.layout.text_tag and fetch its value in PositiveButton's click event. for example:
String text = null;

private void popToTakeText() {
    LayoutInflater inflater = LayoutInflater.from(this);
    View addView = inflater.inflate(R.layout.text_tag, null);

    EditText txtBox = addView.findViewById(R.id.my_edit_text_box);

    final DialogWrapper wrapper = new DialogWrapper(addView);

    new AlertDialog.Builder(this)
            .setTitle("Enter Text")
            .setView(addView)
            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // processAdd(wrapper);
                    // text = wrapper.getData();
                    okBClicked = true;
                    text = txtBox.getText();

                }
            })
            .setNegativeButton("cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // ignore, just dismiss
                        }
                    }).show();

}

