Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing a custom alert dialog on button click

I'm having trouble closing my alert dialog. I am using a layout inflator to make the dialog, so I'm not sure how I would go about closing the thing after I'm done with it. Code is below:

AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this); DialogInterface dia = new DialogInterface();  //Create a custom layout for the dialog box LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);  TextView title = (TextView)layout.findViewById(R.id.rep_1_title); Button add_item = (Button)layout.findViewById(R.id.add_button);  add_item.setOnClickListener(new OnClickListener() {         @Override         public void onClick(View v)         {         //Need this to close the alert dialog box         } });  title.setText(workout_items[position]); dialog.setView(layout); dialog.show(); 

I cant call finish, because that closes the listview that the alert dialog is launched from, and the dialog.dismiss calls are not available to me.

What do you think I should do to fix this?

like image 840
Eric Avatar asked Apr 19 '11 07:04

Eric


People also ask

How do I close a custom dialog?

You may call dismiss(); on the dialog. This work for me.

How do I close alert dialog box?

AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.

How do you prevent a dialog from closing when a button is clicked?

AlertDialog dialog = (AlertDialog) getDialog(); dialog. getButton(AlertDialog. BUTTON_POSITIVE). setEnabled(false);


2 Answers

AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this); DialogInterface dia = new DialogInterface();  //Create a custom layout for the dialog box LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);  TextView title = (TextView)layout.findViewById(R.id.rep_1_title); Button add_item = (Button)layout.findViewById(R.id.add_button);  title.setText(workout_items[position]); dialog.setView(layout);  AlertDialog alertDialog = dialog.create();  add_item.setOnClickListener(new OnClickListener() {     @Override     public void onClick(View v)     {         alertDialog.dismiss();     } });   alertDialog.show(); 
like image 154
Pasha Avatar answered Sep 16 '22 11:09

Pasha


Try this:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);      LayoutInflater inflater = getLayoutInflater();     View dialogView = inflater.inflate(R.layout.brush_opts_dialog,null);      builder.setView(dialogView);       closeBtn = (Button)dialogView.findViewById(R.id.close_btn);      final AlertDialog dialog = builder.create();      closeBtn .setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {             dialog.dismiss();         }     });      dialog.show(); 

You should use the AlertDialog.Builder to "build" the Alert Dialog itself. Then, call the create() method on the AlertDialog.Builder object. This method returns an AlertDialog object, which allows you to call dismiss().

You should not have to create it multiple times, nor use any DialogInterface objects. This works for me. Let me know how it goes for you.

like image 36
Nlinscott Avatar answered Sep 19 '22 11:09

Nlinscott