Question

I have checkboxes for week days in my android application , and I want to put a listener to check if any one of these checkboxes is checked but my way seems hard , isn't there a way to gather all these listeners into one

 sun.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});

    mon.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});
    tue.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});
    wed.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});
    thu.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});
    fri.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});
    sat.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        { if ( isChecked )
            {
            count=count+1;
                // perform logic
            }
        else
        {
            count=count-1;
        }
        }});
Was it helpful?

Solution

You can check the id of the buttonView that is being triggered and do the proper validation, all you have to do is assign the same onCheckListener to the checkboxes and do something as show below:

private OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch(buttonView.getId()){
        case R.id.checkId1:
            //TODO: Code for checked one...
        break;
        case R.id.checkId2:
            //TODO: Code for checked two...
        break;
        }
    }
};

Hope it Helps!

Regards!

OTHER TIPS

If you have similar logic that you haven't provided here, you can create a list of checkboxes and create listeners for them in a cycle, for example.

Let your activity implement the OnCheckedChangeListener and then you have the onCheckChanged...

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

    switch (buttonView.getId()) {
    case R.id.sun:

        break;

    case R.id.mon:

        break;

    default:
        break;
    }

}

there is; implements View.OnClickListener with his method in your class

@Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.checbox1:

                      if(checkbo1.isCheck){
                         //do logic then dont forget to set to the opposite state
                           checkbox.setChecked(false);
                      }
                      else{
                           //do logic
                          checkbox.setChecked(true);
                      }
                      break;
//
            case R.id.checbox2:
                        //do logic etc...
                      break;


        }

    }

then use a switch case deal between different click event from user

hope it help

Of course there is a simpler way. Just make your Activity where you are doing this implement OnCheckedChangeListener

public Class MyActivity extends Activity implements OnCheckedChangeListener{

    //your activity logic in here


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         //your Activity will now receive onCheckedChangeEvents here from
         //the checkboxes to which you assign it as a listener
    }

}

, in onCreate, after you get references to all of your day checkboxes, set the Activity as the listener like this

@Override
public void onCreate(Bundle savedInstanceState){
    //first get references to all your checkboxes and assign them to mon, tue, wed etc.
    //then set the listeners
    mon.setOnCheckedChangeListener(this);
    tue.setOnCheckedChangeListener(this);
    wed.setOnCheckedChangeListener(this);
    thu.setOnCheckedChangeListener(this);
    fri.setOnCheckedChangeListener(this);
    sat.setOnCheckedChangeListener(this);
    sun.setOnCheckedChangeListener(this);
}

, make sure all of your checkboxes have IDs assigned to them in the layout xml, like this

<CheckBox
    android:id="@+id/checkMonday"
 ....

, then you will have one onCheckedChange method where you can handle the different days like this

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    int id = buttonView.getId();
    if(isChecked){
        counter++; //a day has been checked
    }else{
        counter--; //a day has been unchecked
    }
    switch(id){
        case R.id.checkMonday:
             //logic for Monday
             break;

        case R.id.checkTuesday:
             //logic for Tuesday
             break;

        ...
    }
}

That should do the trick!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top