13

This is my code:

    <CheckBox
        android:id="@+id/sprint_checkbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/sprint_game" />

    <CheckBox
        android:id="@+id/marathon_checkbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/marathon" />

    <CheckBox
        android:id="@+id/never_ending_checkbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/never_ending" />

What i want to do is "detect" when one of these is checked and then set the other two to "disable", so the user can select only one at time. I tried to use the ".setOnCheckedChangeListener", but i can't do that, can someone help me with some code? Thanks a lot guys!

Goo
  • 1,318
  • 1
  • 13
  • 31
hyperloris
  • 346
  • 1
  • 7
  • 14
  • ""I tried to use the ".setOnCheckedChangeListener", but i can't do that"" why you can not do that? Place code of your `setOnCheckedChangeListener` – Mohsin Naeem Jul 04 '12 at 15:46
  • The checked change listener is the way to do what you are talking about. – matt5784 Jul 04 '12 at 15:49
  • 2
    why not use a radio button. (i.e. radio group). That is exactly why those are made. Use can only pic one, no need to disable the other two. And by the way onCheckChangedListener works on radio buttons too. – drulabs Jul 04 '12 at 15:59

3 Answers3

29

This is the way you are notified about checked changes:

CheckBox check = findViewById(R.id.sprint_checkbox);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //do stuff

        }
    });

You can also let your activity implements the OnCheckedChangeListener interface and then:

CheckBox check1 = findViewById(R.id.sprint_checkbox);
CheckBox check2 = findViewById(R.id.marathon_checkbox); 
CheckBox check3 = findViewById(R.id.never_ending_checkbox);

check1.setOnCheckedChangeListener(this);
check2.setOnCheckedChangeListener(this);
check3.setOnCheckedChangeListener(this);

Overriding the interface method:

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    switch(buttonView.getId()){
               case R.id.sprint_checkbox:
                 //do stuff
               break;
               case R.id.marathon_checkbox:
                 //do stuff
               break;
               case R.id.never_ending_checkbox:
                //do stuff
               break;

            }

}
Stefano Ortisi
  • 5,288
  • 3
  • 33
  • 41
  • 2
    Thank you. your code sovled my problem while getting boolean values from checkbox. – BK19 Aug 01 '17 at 09:24
1

I believe a RadioButton would be more suitable for your aims.

<RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/radio_group1">
    <RadioButton
        android:id="@+id/sprint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/rad_option1"
        android:checked="true"
    android:onClick="onRadioButtonClick"
        />
    <RadioButton
        android:id="@+id/marathon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/rad_option2"
        android:onClick="onRadioButtonClick"
         />
    <RadioButton
        android:id="@+id/neverending"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/rad_option3"
        android:onClick="onRadioButtonClick"
         />
</RadioGroup>

Then in your code:

public void onRadioButtonClick(View v) {
    RadioButton button = (RadioButton) v;
    // DO YOUR THING HERE DEPENDING ON WHICH RADIOBUTTON IS PICKED
}

In this case, you won't have to deal with disabling other options. The user will have only one option to pick by default.

Erol
  • 6,478
  • 5
  • 41
  • 55
1
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    int id = buttonView.getId();

    if(isChecked){
       switch(id){
          case R.id.sprint:
          //add to database
          ...
       }
    }
    else{
      switch(id){
          case R.id.sprint:
          //remove from database
          ...
       }
   }
}

This should allow you to with ID's. Hope it works.

Twahanz
  • 204
  • 1
  • 15