Suragch
Suragch

Reputation: 512316

Getting the isChecked() value from CheckBox or RadioButton

I have set the click event listener on a number of CheckBox and RadioButton views. Both of these view types have an isChecked() method. I can get the state of either one by casting to its type for every single switch case, but can I get the state just in a general sense for either of them before I enter the switch cases?

@Override
public void onClick(View view) {

    boolean checkState;

    switch (view.getId()) {
        case R.id.radio_button_1:
            checkState = ((RadioButton) view).isChecked();
            // ...
            break;
        case R.id.check_box_1:
            checkState = ((CheckBox) view).isChecked();
            // ...
            break;
        // more check boxes and ratio buttons ...
    }
}

This question has a similar title to this one, but there was too much code there for me to easily understand if they were asking the same thing.

While I was in the process of writing this question, I found an answer, which I will post below. However, if there is a better answer, I will accept it.

Upvotes: 0

Views: 1152

Answers (2)

Suragch
Suragch

Reputation: 512316

Direct Answer

As @MikeM answered in the comments, you can use instanceof to check once for CompoundButton or Checkable and cast to that.

@Override
public void onClick(View view) {

    boolean checkState = false;
    if (view instanceof Checkable) {
        checkState = ((Checkable) view).isChecked();
    }

    switch (view.getId()) {
        // ...
    }
}

Better Answer

However, as @MikeM also mentioned, you should be using an OnCheckedChangeListener rather than an OnClickListener. There are two similar checked changed listeners:

The CompoundButton listener can be used for a CheckBox or a RadioButton. However, if all the radio buttons in a RadioGroup had this set, the listener would get called twice, once for the button getting turned off and once for the button getting turned on. Thus, it is better to use the RadioGroup listener for radio buttons.

So add the RadioGroup.OnCheckedChangeListener to the RadioGroup and the CompoundButton.OnCheckedChangeListener to each CheckBox.

// set RadioGroup.OnCheckedChangeListener
RadioGroup rg = (RadioGroup) view.findViewById(R.id.radio_group);
rg.setOnCheckedChangeListener(radioGroupListener);

// set `CompoundButton.OnCheckedChangeListener`
CheckBox cb1 = (CheckBox) view.findViewById(R.id.check_box_1);
cb1.setOnCheckedChangeListener(checkBoxListener);
CheckBox cb2 = (CheckBox) view.findViewById(R.id.check_box_2);
cb2.setOnCheckedChangeListener(checkBoxListener);
// ...

where the listeners are implemented as follows:

Radio Buttons

The isChecked() value is true for whatever RadioButton id is returned by the RadioGroup listener.

RadioGroup.OnCheckedChangeListener radioGroupListener = new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

        // checkedId is the id of the newly checked radio button
        switch (checkedId) {
            case R.id.radio_button_1:
                // ...
                break;
            case R.id..radio_button_2:
                // ...
                break;
            // ...
        }
    }
};

Check Boxes

The isChecked value is directly passed in to the CompoundButton listener.

CompoundButton.OnCheckedChangeListener checkBoxListener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        switch (compoundButton.getId()) {
            case R.id.checkbox_1:
                // ...
                break;
            case R.id.checkbox_2:
                // ...
                break;
        }
    }
};

Upvotes: 0

Emmanuel Mtali
Emmanuel Mtali

Reputation: 4973

Consider one RadioButton and one CheckBox

RadioButton button = (RadioButton) findViewById(R.id.radio_button);
button.setOnCheckedChangeListener(listener);


CheckBox box = (CheckBox) findViewById(R.id.checkbox);
box.setOnCheckedChangeListener(listener);

Both have a single listener

CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(buttonView instanceof CheckBox ){
                Toast.makeText(getApplicationContext(), "ChechBox " + String.valueOf(isChecked), Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(getApplicationContext(), "RadionButton " + String.valueOf(isChecked), Toast.LENGTH_SHORT).show();
            }
        }
};

If there are many still we can use a single listener as follows

CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(buttonView instanceof CheckBox ){
                final ChechBox b = (ChechBox)buttonView;
                switch(b.getId()){
                   case R.id.first_checkbox:
                   . . . . . . . .
                }
            }else {
                final RadioButton r = (RadioButton)buttonView;
                switch(r.getId()){
                    case R.id.first_radio
                    . . . . . . . . . . . 
                }
            }
        }
};

You need to attach all of the views to this listener

Upvotes: 1

Related Questions