mardi 18 avril 2017

CheckBox within an AlertDialog

I have a following piece of code

   public void showAlert(final Context context) {

    mContext = context;
    AlertDialog.Builder builder = new AlertDialog.Builder(context);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService( 
    Context.LAYOUT_INFLATER_SERVICE );
    View view = inflater.inflate(R.layout.alert_dialog_layout,null);
    builder.setView(view);
    final CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkBox);

    try {

        checkBox.setOnCheckedChangeListener(
                new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, 
                      boolean isChecked) {
                        if (isChecked) {
                            Log.d(getClass().getSimpleName(), "CheckBox is 
                          CHecked");
                        } else {
                            Log.d(getClass().getSimpleName(), "CheckBox is 
                         Not Checked");
                        }
                        Log.d(getClass().getSimpleName(), "Inside ChecBox 
                       CheckedListner");
                    }
                }
        );
    }
    catch (Exception e){
        Log.d("TAG","Exception-->"+e);
    }


    builder.setPositiveButton(PositiveButton, new 
   DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

       //some code here:

       }
       });

    builder.setNegativeButton(NegativeButton, new 
    DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    final AlertDialog alert = builder.create();
    alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    alert.show();
   }

Whenever someone press the CheckBox it closes the Alert Dialog, which i dont want. Actually when user click the checkbox i have some data which will be passed to a another listener(an Interface) but my dialog is getting closed when checkbox is clicked.

Can anybody tell me how to keep the alertdialog intact even if somebody click the checkbox.




Aucun commentaire:

Enregistrer un commentaire