Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a Dialog with radio buttons by clicking on a radiobutton

It feels like a very stupid question but i cant figure out how to close my dialog when the user clicks a radiobutton in the dialog.. (so something like alert.dismiss() in the onClick)

final CharSequence[] items = {"Nederland", "België", "Denemarken", "Duitsland", "Frankrijk", "Italië", "Luxemburg", "Oostenrijk", "Verenigd Koningkrijg", "Zweden"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Selecteer het land");
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {

                Button country = (Button) findViewById(R.id.land_fac_but);
                country.setText(items[item]);
            }
        });

        AlertDialog alert = builder.create();   
        alert.show();
like image 598
Luciano Avatar asked Dec 21 '22 05:12

Luciano


1 Answers

Just add dialog.dismiss() inside your OnClickListener:

builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int item) {
    Button country = (Button) findViewById(R.id.land_fac_but);
    country.setText(items[item]);
    dialog.dismiss();
  }
});

If you want to dismiss it as soon as an item is clicked. Else you might want to look into creating a more custom Dialog by extending AlertDialog.

like image 128
kaspermoerch Avatar answered Jan 31 '23 01:01

kaspermoerch