How to prevent alertdialog.builder from closing if an option is not selected?

1

I want to avoid that the AlertDialog.Builder is closed if an option is not selected, and probe removing the dialog.dismiss() but the same when I click on the button accept without having selected an option closes, I want the AlertDialog to remain visible there and when you press the OK button without having selected an option that displays a Toast saying that you must select an option.

imageButtonAddNewClient.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String itemOptions[] = {"Cliente activo", "Cliente inactivo"};
            new AlertDialog.Builder(NewClient.this, R.style.AlertDialogCustom)
                    .setTitle("Agregar nuevo cliente:")
                    .setSingleChoiceItems(itemOptions, posicionSeleccionadaStateClient, null)
                    .setCancelable(false)
                    .setNeutralButton("Cancelar", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            intent = new Intent(NewClient.this, ActivityNewClient.class);
                            posicionSeleccionadaStateClient = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
                            if(posicionSeleccionadaStateClient == 0){
                                intent.putExtra("code", 0);
                            }else if(posicionSeleccionadaStateClient == 1){
                                intent.putExtra("code", 1);

                            }else{
                                Toast.makeText(NewClient.this, "Seleccione una opción.", Toast.LENGTH_SHORT).show();
                            }
                            startActivity(intent);
                            dialog.dismiss();
                        }
                    }).show();
        }
    });
    
asked by Leonidas 01.01.2019 в 18:07
source

1 answer

1

To achieve that, what you have to do is overwrite the onClick of the accept button, there to validate that the data is as you want and according to what to do or not the dismiss.

First you have to set the onclick of the positive button in null because we are going to write about it.

AlertDialog dialog = new AlertDialog.Builder(NewClient.this, R.style.AlertDialogCustom)
   .setTitle("Agregar nuevo cliente:")
   .setSingleChoiceItems(itemOptions, posicionSeleccionadaStateClient, null)
   .setCancelable(false)
   .setNeutralButton("Cancelar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
   .setPositiveButton("Aceptar", null);

Now we add an OnShowListener to be able to overwrite the onclick after the dialog is created.

dialog.setOnShowListener(new DialogInterface.OnShowListener() {

   @Override
   public void onShow(DialogInterface dialogInterface) {

      Button button = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {

            if (todo ok?){ //Aca valida lo que necesites
               dialog.dismiss();
            }
         }
      });
   }
});
dialog.show();
    
answered by 01.01.2019 / 20:29
source