RadioGroup calls onCheckChanged() three times
Asked Answered
A

5

26

I've been struggling with an issue of programmatic checking of a RadioButton, which is situated in a RadioGroup. It seems that a single check() call raises three events - once for the first RadioButton and twice for the second, which is my target. At the same time clicking on the second RadioButton in the interface causes only one event to appear, which is correct.

So, is there a way to avoid multiple event raising ?

public class RadiogroupActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        RadioGroup r = (RadioGroup) findViewById(R.id.radioGroup1);
        RadioButton b = (RadioButton) findViewById(R.id.radio1);
        r.setOnCheckedChangeListener(onPromobuttonsClick);
        r.check(R.id.radio1);

    }

    private OnCheckedChangeListener onPromobuttonsClick = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Log.d("x", "x");
        }
    };
}
Ahouh answered 21/4, 2012 at 22:58 Comment(0)
S
54

So, is there a way to avoid multiple event raising ?

Call b.setChecked(true); instead.


A quick look through the source code confirms that RadioGroup#check() really does call the OnCheckChangedListener three times...

  1. When the current selection is unchecked,
  2. When the new RadioButton is checked, and
  3. When the RadioGroup saves which RadioButton is now checked.

Odd quirk.

Slumlord answered 21/4, 2012 at 23:45 Comment(4)
Sir : Need your help .Take a look please #22603059Urine
Really saved my life... Superb @SlumlordCyprinid
Please elaborate answer more . I am not getting your answer.Kelci
Yes, really save my time Thanks @SlumlordVertumnus
K
4

Simply post your r.setOnCheckedChangeListener(onPromobuttonsClick); line in onResume() method. It is works for me.

Kelci answered 5/5, 2016 at 6:45 Comment(0)
P
2

So this is an old one but I also got a similar behavior. A workaround that I found was to call the toggle() method of RadioButton instead of the check() method of RadioGroup.

So in this example, you woulod just need to swap

r.check(R.id.radio1);

with

b.toggle();

since b is already the view with id R.id.radio1.

Peggypegma answered 13/9, 2016 at 18:10 Comment(0)
K
1

I had this problem when I wanted to log analytics for radio button presses, but the way the radio groups calls were being handled with .check() caused onCheckedChangeListener to get called multiple times (which sent too many events).

I handled it the following way to only get one event per click:

// create listeners so we don't duplicate the creation in for loop
View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // whatever you want to do here
    }
};

// add on click listener to all radio buttons
int buttonCount = buttonGroup.getChildCount();
for (int i = 0; i < buttonCount; i++) {
    if (buttonGroup.getChildAt(i) instanceof RadioButton) {
        buttonGroup.getChildAt(i).setOnClickListener(onClickListener);
    }
}
Kiki answered 13/1, 2017 at 19:7 Comment(0)
F
0

This is a old question but I didn't found a complete answer yet that cover my issue. But thanks to #Hellojeffy suggestion I figured a way to handle the situation when RadioButton trigger multipletimes when using setOnCheckedChangeListener(). The idea is to add a click on each view inside the RadioGroup and trigger a event on click, instead a event on check changes.

for (child in radioButtonGroup.children) {
            if (child is RadioButton) {
                when (child.id) {
                    R.id.rb1 -> child.setOnClickListener { onRb1Click(...) }
                    R.id.rb2 -> child.setOnClickListener { onRb1Click(...) }
                    R.id.rb3 -> child.setOnClickListener { onRb1Click(...) }
                        ...
                }
            }
        }
Faction answered 5/1, 2022 at 19:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.