Dismiss a custom dialog in the onClick handler of the dialog's Button and read dialog element's value
<p>This is very much a repeat of an old question that is available here:
<a href="http://stackoverflow.com/questions/2825272/dismiss-a-custom-dialog">Dismiss a custom dialog?</a></p>

<p>While I understand the accepted solution, it does not seem very right to have a class member hold a reference to the dialog instance and dismiss it later using parent.this.dialog.dismiss().</p>

<p>Also, I do no understand the second solution i.e. using:
dismissDialog(DIALOG_ID);</p>

<p>I've read that part of the docs over and over but I don't quite understand why one needs to implement OnCreateDialog etc in the parent activity when I'm able to do all i want (except for dismissing) using this in the parent activity's OnCreate:</p>

<pre><code>ImageButton btnAdd = (ImageButton)findViewById(R.id.button_shoppinglists_add);
btnAdd.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
     Dialog dialog = new Dialog(ShoppingListActivity.this,
                   R.style.Theme_AppCustomDialog);
 dialog.setContentView(R.layout.dialog_addshoppinglist);
 dialog.setCancelable(true);    

 // Set the on OK listener
 Button okButton = (Button) dialog.findViewById(R.id.dialog_button_OK);
 if (okButton != null) {
     okButton.setOnClickListener(new OnClickListener() {

         public void onClick(View v) {
                 Log.e("ShoppingListActivity:AddList", "OK");

                 // How to dismiss the dialog here?????
                 // doing the following does not feel right:
                 // parentActivity.this.mCustomDialog.dissmiss()
                 // where mCustomDialog holds a reference to the
                 // dialog just created
     }
 });
 }
</code></pre>

<p>Edit 1: It also occurred to me that I don't know how to read the value of an EditText field that may be in the dialog as I don't have a reference to the dialog in the button's onClick handler.</p>

<p>Edit 2: Did a lot of digging around, it doesn't seem to be possible to use AlertDialog like I wanted. As suggested, I've changed my implementation to use AlertDialog instead:</p>

<pre><code>ImageButton btnAdd = (ImageButton) findViewById(R.id.button_shoppinglists_add);
btnAdd.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
    // inflate the view from resource layout
        LayoutInflater  inflater = (LayoutInflater) 
            shoppingListActivity.this.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);

    final View dialogView = inflater.inflate(
            R.layout.dialog_addshoppinglist, (ViewGroup)       
                findViewById(R.id.dialog_addshoppinglist_root));

        AlertDialog dialog = new AlertDialog.Builder(ShoppingListActivity.this)
            .setView(dialogView)
        .setTitle(getResources().getString(R.string.shoppinglist_add_title))
            .create();

    dialog.setButton(DialogInterface.BUTTON1, "OK", 
            new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                EditText editListName = (EditText) dialogView.findViewById(
                        R.id.dialog_addshoppinglist_editTextName);
            String listName = editListName.getText().toString();

            dialog.dismiss();
                }
    });
        }
    }
</code></pre>

<p>The trick, as it turned out, is to use the final keyword on your view's object like so:</p>

<pre><code>final View dialogView = inflater.inflate(
    R.layout.dialog_addshoppinglist, (ViewGroup)       
    findViewById(R.id.dialog_addshoppinglist_root));
</code></pre>

<p>Now the dialogView object is available in the DialogInterface.onClickListener. As you may have guessed, I'm new to JAVA :)</p>

<p>Details documented <a href="http://geekjamboree.wordpress.com/2011/11/19/dialog-vs-alertdialog/" rel="nofollow">here</a></p>

<p>Thanks
M@nish</p>
