Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6851495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:15:17+00:00 2026-05-27T01:15:17+00:00

This is very much a repeat of an old question that is available here:

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:15:18+00:00Added an answer on May 27, 2026 at 1:15 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Important : Please see this very much related question: Return multiple values in C++
Very much related to my previous question , but I found this to be
I'm sorry for this very newbish question, I'm not much given into web development.
I'm very much a vb person, but have had to use this id number
This is my situation, very much simplified. My classes; public class ClassBase { [Key]
This seems very noisy to me. Five lines of overhead is just too much.
This is pretty much the same problem i have, except with very different code:
This very strange. When I resize a WinForms dialog the controls are flickering very
PROBLEM SOLVED: I was overlooking a very simple issue, and that is that this
I'll start by saying I am very much a beginner programmer, this is essentially

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.