2

I have a multiple checkbox and a button. What should I do to disable the button if none of the checkbox is check and enable the button if it's checked?

reson90
  • 137
  • 2
  • 5
  • 11

7 Answers7

8

Try with this,

 Button mButton=(Button)findViewById( R.id.button01);
    CheckBox mCheckBox= ( CheckBox ) findViewById( R.id.checkbox01);
    mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if ( isChecked )
            {
              mButton.setEnabled(true);

            }else{
  mButton.setEnabled(false);
}

        }
    });
Aerrow
  • 12,086
  • 10
  • 56
  • 90
3

When you enter in that view make the button disabled (in your XML), and whenever user hit any of the check-boxes manage one global variable e.g if the global count is > 1 then make the button enable in that activity.

Manage the global variable in a way that if user is turning on the check box then increment it and if he is turning off the checkbox decrease the counter.

I hope you got the concept.

Basically it is all about managing the count how many checkboxes are turned on; if more then one is turned on make the button enabled else make it disabled.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Jigar Pandya
  • 6,004
  • 2
  • 27
  • 45
2

Instead of enabling and disabling the button you can use setVisibility() method for button. In the following manner.

Button btn =(Button)findViewById( R.id.mybutton);
CheckBox checkBox= ( CheckBox ) findViewById( R.id.checkbox01);
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
          btn.setVisibility(VISIBLE);
        }
          else{
                btn.setVisibility(GONE);
               }
     }
});

By using this method you can set the visibility of your view.Your button will be visible only if checkBox is cheaked otherwise your button will not be visible.Let me know it works or not for you.

Akshay
  • 2,506
  • 4
  • 34
  • 55
  • When I use this `checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){...}` in my code, I get this error `The method setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener) in the type CompoundButton is not applicable for the arguments (new OnCheckedChangeListener(){})` Any idea/help as to what I would be missing? – VikramV Dec 27 '13 at 05:28
0

Use this:

myButton.setEnabled(false);     

See this question for more details.

Community
  • 1
  • 1
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
0

you can disable button by using below code.

mBtn.setEnabled(false);

and can enable it later by using below code

mBtn.setEnabled(true);
AAnkit
  • 27,299
  • 12
  • 60
  • 71
0
    mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {        
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mButton.setEnabled(isChecked);
        }
    });

as simple as that :)

Ankit Popli
  • 2,809
  • 3
  • 37
  • 61
herrzura
  • 1
  • 4
0

try this termsAndConditionsCheckBox.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (termsAndConditionsCheckBox.isChecked() && privacyPolicyCheckBox.isChecked()){
                agreebutton.setEnabled(true);
                agreebutton.setTextColor(getResources().getColor(android.R.color.white));
                agreebutton.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
            }
            else {
                agreebutton.setEnabled(false);
                agreebutton.setTextColor(getResources().getColor(android.R.color.white));
                agreebutton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
            }
        }   
    });
    privacyPolicyCheckBox.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (termsAndConditionsCheckBox.isChecked() && privacyPolicyCheckBox.isChecked()){
                agreebutton.setEnabled(true);
                agreebutton.setTextColor(getResources().getColor(android.R.color.white));
                agreebutton.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
            }
            else {
                agreebutton.setEnabled(false);
                agreebutton.setTextColor(getResources().getColor(android.R.color.white));
                agreebutton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
            }

        }
    });