Dismiss AlertDialog.Builder from OnClick

trevor-e picture trevor-e · Jul 26, 2011 · Viewed 53.6k times · Source

I'm trying to make it so that a dialog pops up for users which has two buttons in the body and a cancel button at the bottom. When a user clicks one of the two buttons the dialog will disappear, and hitting cancel will just cancel out of the dialog. The cancel part works fine, but I can't figure out how to dismiss the dialog manually. Here's my code:

public void onItemClick(AdapterView<?> parent, View view,
                    final int position, long id) {

                Context mContext = getApplicationContext();
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
                View layout = inflater.inflate(R.layout.config_dialog,
                        (ViewGroup) findViewById(R.id.config_dialog));

                Button connect = (Button) layout.findViewById(R.id.config_connect);
                Button delete = (Button) layout.findViewById(R.id.config_delete);

                alert = new AlertDialog.Builder(Configuration.this);
                alert.setTitle("Profile");

                connect.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        trace("Connect" + Integer.toString(position));
                        toast("Connected");
                        SharedPreferences app_preferences = 
                                PreferenceManager.getDefaultSharedPreferences(Configuration.this);
                        SharedPreferences.Editor editor = app_preferences.edit();
                        editor.putString("IP", fetch.get(position).IP);
                        editor.commit();
                        //Add dismiss here


                    }

                });

                delete.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {

                        trace("Delete");

                    }

                });


                // Set layout 
                alert.setView(layout);

                alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Canceled.
                    }
                });

                alert.show();

When I try to add the alert.dismiss(), Eclipse gives me an error. .dismiss() also doesn't show up in alert's autocomplete list.

Answer

theisenp picture theisenp · Jul 26, 2011

Merlin's answer is correct and should be accepted, but for the sake of completeness I will post an alternative.

The problem is that you are trying to dismiss an instance of AlertDialog.Builder instead of AlertDialog. This is why Eclipse will not auto-complete the method for you. Once you call create() on the AlertDialog.Builder, you can dismiss the AlertDialog that you receive as a result.

public class AlertDialogTestActivity extends Activity
{

    AlertDialog alert;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button connect = new Button(this);
        connect.setText("Don't push me");

        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
        alertBuilder.setTitle("Profile");
        alertBuilder.setView(connect);


        connect.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                alert.dismiss();
            }
        });

        alert = alertBuilder.create();
    }
}