The question:
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?
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
You can do this:
satView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
}
}
);
Method 2
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");
}
}
});
Method 3
You get the error because you imported wrong package.You should import android.widget.CompoundButton.OnCheckedChangeListener;
So the callback should be :
box.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
}
});
Method 4
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.
Method 5
Translation of the accepted answer by Chris into Kotlin:
val checkBox: CheckBox = findViewById(R.id.chk)
checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
// Code here
}
Method 6
try this
satView.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (isChecked){
// perform logic
}
}
});
Method 7
Change RadioGroup group
with CompoundButton buttonView
and then press Ctrl+Shift+O to fix your imports.
Method 8
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;
}
}
}
}
Method 9
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");
}
}
});
Method 10
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")
}
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0