143

I want to put a Listener over a CheckBox. I looked for info and it is like this:

satView = (CheckBox)findViewById(R.id.sateliteCheckBox);

satView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (isChecked){
            // perform logic
        }
    }

});

The problem is that Eclipse thinks it's an OnCheckedChangeListener for a RadioGroup and it doesn't work. How can I fix this?

10 Answers 10

317

You can do this:

satView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

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

       }
   }
);     
3
  • 1
    Thank you and here is how to remove it: stackoverflow.com/questions/18881817/…
    – Michal
    Jan 27, 2014 at 11:43
  • OnCheckedChangeListener and CompoundButton.OnCheckedChangeListener are different.
    – zionpi
    Apr 30, 2015 at 6:25
  • 1
    for kotlin satView.setOnCheckedChangeListener { buttonView, _ -> if (buttonView.isChecked) { // perform action } else { // perform action } } Nov 18, 2019 at 10:27
61

you may also go for a simple View.OnClickListener:

satView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(((CompoundButton) view).isChecked()){
            System.out.println("Checked");
        } else {
            System.out.println("Un-Checked");
        }
    }
});
2
  • 3
    It's really only one working solution when I wanted to listen a checkbox click action, not its state like others solutions recommend there.
    – Acuna
    Dec 16, 2017 at 21:49
  • the best way to set check listener for checkbox in recycleView Aug 9, 2021 at 3:48
22

You get the error because you imported wrong package.You should import android.widget.CompoundButton.OnCheckedChangeListener;

So the callback should be :

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        // Perform logic
    }
});
0
12

Try this:

satView = (CheckBox) findViewById(R.id.sateliteCheckBox);

satView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          if (buttonView.isChecked()) { 
                // checked
          } 
          else 
          {
                // not checked
          }
    }

});

Hope this helps.

8

Translation of the accepted answer by Chris into Kotlin:

val checkBox: CheckBox = findViewById(R.id.chk)
checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
    // Code here 
}
1
  • 1
    Note: If the buttonView is not used: "{ _, isChecked ->"
    – Elletlar
    Apr 25, 2018 at 13:59
4

try this

satView.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (isChecked){
                // perform logic
            }
        }

    });
4

Change RadioGroup group with CompoundButton buttonView and then press Ctrl+Shift+O to fix your imports.

4

You could use this code.

public class MySampleActivity extends Activity {
    CheckBox cb1, cb2, cb3, cb4;
    LinearLayout l1, l2, l3, l4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cb1 = (CheckBox) findViewById(R.id.cb1);
        cb2 = (CheckBox) findViewById(R.id.cb2);
        cb3 = (CheckBox) findViewById(R.id.cb3);
        cb4 = (CheckBox) findViewById(R.id.cb4);
        l1 = (LinearLayout) findViewById(R.id.l1);
        l2 = (LinearLayout) findViewById(R.id.l2);
        l3 = (LinearLayout) findViewById(R.id.l3);
        l4 = (LinearLayout) findViewById(R.id.l4);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(1));
        cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(2));
        cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(3));
        cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(4));
    }

    public class MyCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
        int position;

        public MyCheckedChangeListener(int position) {
            this.position = position;
        }

        private void changeVisibility(LinearLayout layout, boolean isChecked) {
            if (isChecked) {
                l1.setVisibility(View.VISIBLE);
            } else {
                l1.setVisibility(View.GONE);
            }

        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch (position) {
                case 1:
                    changeVisibility(l1, isChecked);
                    break;
                case 2:
                    changeVisibility(l2, isChecked);
                    break;
                case 3:
                    changeVisibility(l3, isChecked);
                    break;
                case 4:
                    changeVisibility(l4, isChecked);
                    break;
            }
        }
    }
}
1
  • Why are you passing a 'LinearLayout' to your 'changeVisibility(LinearLayout layout, boolean isChecked)' method yet you're only changing one layout's visibility each time? Feb 5, 2018 at 15:54
2
h.chk.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View view)
    {
        CheckBox chk=(CheckBox)view; // important line and code work
        if(chk.isChecked())
        {
            Message.message(a,"Clicked at"+position);
        }
        else
        {
            Message.message(a,"UnClick");
        }
    }
});
1
  • 1
    Welcome to Stack Overflow! You can make this answer better by editing it to explain how does it work.
    – SE is dead
    Aug 20, 2016 at 8:01
1

If you are looking to do this in Kotlin with the interface implementation.

class MainActivity: AppCompatActivity(),CompoundButton.OnCheckedChangeListener{

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

      val yourCheckBox = findViewById<CheckBox>(R.id.check_box)
      yourCheckBox.setOnCheckedChangeListener(this)

    }

 override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        when(buttonView?.id){
            R.id.check_box -> Log.d("Checked: ","$isChecked")
        }
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.