141

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?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207

10 Answers10

315

You can do this:

satView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

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

       }
   }
);     
primax79
  • 428
  • 2
  • 14
Chris
  • 22,923
  • 4
  • 56
  • 50
  • 1
    Thank you and here is how to remove it: http://stackoverflow.com/questions/18881817/removing-strikethrough-from-textview – Michal Jan 27 '14 at 11:43
  • `OnCheckedChangeListener` and `CompoundButton.OnCheckedChangeListener` are different. – zionpi Apr 30 '15 at 06:25
  • 1
    for kotlin satView.setOnCheckedChangeListener { buttonView, _ -> if (buttonView.isChecked) { // perform action } else { // perform action } } – Aminul Haque Aome Nov 18 '19 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");
        }
    }
});
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
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
    }
});
cegprakash
  • 2,937
  • 33
  • 60
Huang
  • 4,812
  • 3
  • 21
  • 20
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.

Edric
  • 24,639
  • 13
  • 81
  • 91
oriolpons
  • 1,883
  • 12
  • 20
8

Translation of the accepted answer by Chris into Kotlin:

val checkBox: CheckBox = findViewById(R.id.chk)
checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
    // Code here 
}
Elletlar
  • 3,136
  • 7
  • 32
  • 38
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;
            }
        }
    }
}
Arun Shankar
  • 612
  • 1
  • 5
  • 16
  • 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? – Bradley Wilson Feb 05 '18 at 15:54
4

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

SERPRO
  • 10,015
  • 8
  • 46
  • 63
4

try this

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

    });
confucius
  • 13,127
  • 10
  • 47
  • 66
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");
        }
    }
});
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
Swapnil Thube
  • 103
  • 1
  • 5
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")
        }
    }

}
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77