Rao's
Rao's

Reputation: 1097

How to Enable or Disable a checkbox in android

I have 4 checkbox in my quickalert dialog... What I want is, when I check[enable] 1 checkboxs other 3 checkbox must get Disable.. I do them clicked, but not disabled. And I have no idea how to do that.

Here is what I am looking for:

mockup

    checkmrn  =  (CheckBox) container.findViewById(R.id.MrnCheckBox);
    checkmrn.setText("By Mrn");
    checkmrn.setEnabled(false);
    checkdate  =  (CheckBox) container.findViewById(R.id.dateCheckBox);
    checkdate.setText("By Date");
    checkname  =  (CheckBox) container.findViewById(R.id.nameCheckBox);
    checkname.setText("By Name");
    checklocation  =  (CheckBox) container.findViewById(R.id.locationCheckBox);
    checklocation.setText("By Location");
    okButton = (Button) container.findViewById(R.id.okButton);
    okButton.setText("Ok");

    checkmrn.setOnClickListener(new OnClickListener() {          
          @Override
          public void onClick(View v) {                  
            if (((CheckBox) v).isChecked()) {
                Toast.makeText(mContext, "By Mrn", Toast.LENGTH_SHORT).show();
            }        
          }
        });
    checkdate.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                  
            if (((CheckBox) v).isChecked()) {
                Toast.makeText(mContext, "By Date", Toast.LENGTH_SHORT).show();
            }        
          }
        });
    checkname.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                  
            if (((CheckBox) v).isChecked()) {
                Toast.makeText(mContext, "By Name", Toast.LENGTH_SHORT).show();
            }        
          }
        });
    checklocation.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                  
            if (((CheckBox) v).isChecked()) {
                Toast.makeText(mContext, "By Location", Toast.LENGTH_SHORT).show();
            }        
          }
        });

Upvotes: 2

Views: 20610

Answers (7)

Rathohan
Rathohan

Reputation: 1

You can use the property of the checkBox which is setClickable.

The following is a simple code example which lets a checkbox be disabled:

CheckBox check_1;
check_1 = (CheckBoX)findViewById(R.id.(the id of your checkbox in xml));
check_1.setClickable(false);
//then this checkbox will be disabled.

Upvotes: 0

Madi
Madi

Reputation: 1855

When you click other layout or button etc. you can add this line code ->

  1. in Kotlin: yourCheckbox.isChecked = !yourCheckbox.isChecked

  2. in Java: yourCheckbox.setChecked() = !yourCheckbox.isChecked()

Hope this may help you :)

Upvotes: 0

Rao's
Rao's

Reputation: 1097

Here is the working code for the above question.

  checkmrn  =  (CheckBox) container.findViewById(R.id.MrnCheckBox);
    checkmrn.setText("By Mrn"); 
    checkdate  =  (CheckBox) container.findViewById(R.id.dateCheckBox);
    checkdate.setText("By Date");   
    checkname  =  (CheckBox) container.findViewById(R.id.nameCheckBox);
    checkname.setText("By Name");
    checklocation  =  (CheckBox) container.findViewById(R.id.locationCheckBox);
    checklocation.setText("By Location");

    okButton = (Button) container.findViewById(R.id.okButton);
    okButton.setText("Ok");

    checkmrn.setOnClickListener(new OnClickListener() {          
          @Override
          public void onClick(View v) {                  

              if (((CheckBox) v).isChecked()) {
                  checkmrn.setChecked(true);
                  Toast.makeText(mContext, "By Mrn", Toast.LENGTH_SHORT).show();
                  checkdate.setChecked(false);
                  checkname.setChecked(false);
                  checklocation.setChecked(false);
                    } 
          }
        });
    checkdate.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                   
              if (((CheckBox) v).isChecked()) {
                  checkdate.setChecked(true);
                  Toast.makeText(mContext, "By Date", Toast.LENGTH_SHORT).show();
                  checkmrn.setChecked(false);
                  checkname.setChecked(false);
                  checklocation.setChecked(false);
                    } 
          }
        });
    checkname.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                                     
              if (((CheckBox) v).isChecked()) {
                  checkname.setChecked(true);
                  Toast.makeText(mContext, "By Name", Toast.LENGTH_SHORT).show();
                  checkmrn.setChecked(false);
                  checkdate.setChecked(false);
                  checklocation.setChecked(false);
                    }  
          }
        });
    checklocation.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                  

              if (((CheckBox) v).isChecked()) {
                  checklocation.setChecked(true);
                  Toast.makeText(mContext, "By Location", Toast.LENGTH_SHORT).show();
                  checkmrn.setChecked(false);
                  checkname.setChecked(false);
                  checkdate.setChecked(false);
                    } 
          }
        });
checkmrn  =  (CheckBox) container.findViewById(R.id.MrnCheckBox);
    checkmrn.setText("By Mrn"); 
    checkdate  =  (CheckBox) container.findViewById(R.id.dateCheckBox);
    checkdate.setText("By Date");   
    checkname  =  (CheckBox) container.findViewById(R.id.nameCheckBox);
    checkname.setText("By Name");
    checklocation  =  (CheckBox) container.findViewById(R.id.locationCheckBox);
    checklocation.setText("By Location");

    okButton = (Button) container.findViewById(R.id.okButton);
    okButton.setText("Ok");

    checkmrn.setOnClickListener(new OnClickListener() {          
          @Override
          public void onClick(View v) {                  

              if (((CheckBox) v).isChecked()) {
                  checkmrn.setChecked(true);
                  Toast.makeText(mContext, "By Mrn", Toast.LENGTH_SHORT).show();
                  checkdate.setChecked(false);
                  checkname.setChecked(false);
                  checklocation.setChecked(false);
                    } 
          }
        });
    checkdate.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                   
              if (((CheckBox) v).isChecked()) {
                  checkdate.setChecked(true);
                  Toast.makeText(mContext, "By Date", Toast.LENGTH_SHORT).show();
                  checkmrn.setChecked(false);
                  checkname.setChecked(false);
                  checklocation.setChecked(false);
                    } 
          }
        });
    checkname.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                                     
              if (((CheckBox) v).isChecked()) {
                  checkname.setChecked(true);
                  Toast.makeText(mContext, "By Name", Toast.LENGTH_SHORT).show();
                  checkmrn.setChecked(false);
                  checkdate.setChecked(false);
                  checklocation.setChecked(false);
                    }  
          }
        });
    checklocation.setOnClickListener(new OnClickListener() {             
          @Override
          public void onClick(View v) {                  

              if (((CheckBox) v).isChecked()) {
                  checklocation.setChecked(true);
                  Toast.makeText(mContext, "By Location", Toast.LENGTH_SHORT).show();
                  checkmrn.setChecked(false);
                  checkname.setChecked(false);
                  checkdate.setChecked(false);
                    } 
          }
        });

Upvotes: 5

Vito
Vito

Reputation: 1434

Just use RadioButton in RadioGroup!!! It is what you want!

Upvotes: 0

Vito
Vito

Reputation: 1434

Try like this:

private OnCheckedChangeListener listener = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked){
switch(arg0.getId())
  {
    case R.id.firstCheckBox:
         firstCheckBox.setChecked(true);
         secondCheckBox.setChecked(false);
         thirdCheckBox.setChecked(false);
         break;
    case R.id.secondCheckBox:
         firstCheckBox.setChecked(false);
         secondCheckBox.setChecked(true);
         thirdCheckBox.setChecked(false);
         break;
   case R.id.thirdCheckBox:
        firstCheckBox.setChecked(false);
        secondCheckBox.setChecked(false);
       thirdCheckBox.setChecked(true);
        break;
  }
}

}
};

Upvotes: 0

Ankitkumar Makwana
Ankitkumar Makwana

Reputation: 3485

when click on Checkbox you can make other checkbox false and current is true like

           if (((CheckBox) v).isChecked()) {
            currentchk.setChecked(true);
             otherchkbox.setChecked(false);
            }        

Upvotes: 0

If you want to disable other options when one is selected create disable/enable method

protected void setGroupState(boolean state)
{
     //here put all options to have state changed in one go
}

then in the click handler of the option that should change the state of the "group"

call it with proper state. e.g. setGroupState(false);

best regards

Upvotes: 0

Related Questions