Question

I have two checkboxes, Now I want to show a alertdialog when both are checked and if only one is checked then I don't want to show user any info.

For this purpose I have created below code in oncreate but nothing is happening when run the application.

if (Percentage.isChecked()==true &&  Value.isChecked()==true)
        {
            AlertDialog.Builder showdialog=new AlertDialog.Builder(getApplicationContext());
            showdialog.setTitle("Check Alert");
            showdialog.setMessage("You can't select both options");
            showdialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    
                }

                
            });
            
            showdialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    
                }
            });
            
            showdialog.show();
        }

Is my approach wrong? If so, what changes do I need to make to fix this?

Was it helpful?

Solution

Solved on my own... I'm answering so that anyone who has the same issue can use this.

Create one method to capture the checked state of the code:

public void showalert(){

        if (Percentage.isChecked()==true &&  Value.isChecked()==true)
        {
            AlertDialog.Builder showdialog=new AlertDialog.Builder(this);
            showdialog.setTitle("Check Alert");
            showdialog.setMessage("You can't select both options");
            showdialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Percentage.setChecked(false);
                    Value.setChecked(false);
                }


            });

            showdialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Percentage.setChecked(false);
                    Value.setChecked(false);
                }
            });

            showdialog.show();
        }
    }

Now for individual checkboxes invoked the method setOnCheckedChangeListener in that called the above created method.

Percentage.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                showalert();

            }
        });

        Value.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
            showalert();

            }
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top