This is very much a repeat of an old question that is available here:
Dismiss a custom dialog?
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().
Also, I do no understand the second solution i.e. using:
dismissDialog(DIALOG_ID);
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:
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
}
});
}
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.
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:
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();
}
});
}
}
The trick, as it turned out, is to use the final keyword on your view’s object like so:
final View dialogView = inflater.inflate(
R.layout.dialog_addshoppinglist, (ViewGroup)
findViewById(R.id.dialog_addshoppinglist_root));
Now the dialogView object is available in the DialogInterface.onClickListener. As you may have guessed, I’m new to JAVA 🙂
Details documented here
Thanks
M@nish
Instead of using View.OnClickListener use DialogInterface.OnClickListener and inside the new implementation of the onClick() method call dialog.dismiss(), because you already will have dialog as the parameter of the onClick method.
Let me know if you have more doubts.
Thanks.