Question

I am having a problem with a radio button that is in a radio group.

I have an app that is a quiz application and I ask 5 questions.

When run the application in my Android emulator all the questions have no problem but only on the 3rd question. When I click with my mouse on the radio button it seems toggle to a checked state but then it unchecks right away. Has anyone seen this kind of behavior?!

I setup this Radio group and dynamically add 4 radio buttons in a radio group and then use an OnCheckedChangeListener() event to capture the change.

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                for(int i=0; i<=3;i++)
                {
                    RadioButton btn = (RadioButton) radioGroup.getChildAt(i);

                    if (btn.isPressed() && questNo < 6)
                    {

                        if (corrAns[questNo-1].equals(btn.getText()) && flag==true)
                        {
                            Log.e(LOG_TAG,"onCheckedChanged: correct answer = btn Text");
                            score++;
                            flag = false;
                            checked = true; 
                        }
                        else if(checked==true)
                        {
                            Log.e(LOG_TAG,"onCheckedChanged: correct answer != btn Text");
                            score--;
                            flag = true;
                            checked=false;
                        }
                    }
                }

                Log.e(LOG_TAG, "Score:"+ Integer.toString(score));
            }
        });

I have noticed that it happens randomly on different questions and only on first radiobutton that is selected but if you select another one after then the functionality returns to normal. Any ideas?

Was it helpful?

Solution

I had the same bug. I'm also using dynamic radioGroups and buttons. This is working for me:

private OnCheckedChangeListener rblLikert_Listener = new OnCheckedChangeListener() {
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int selectedIndex = group.getCheckedRadioButtonId();

        if(selectedIndex != -1)
        {
            m_likertValue = radioButtonValue;
            int buttonId = group.getCheckedRadioButtonId();
            Logger.i("button id: " + String.valueOf(buttonId));
            RadioButton selectedButton = (RadioButton)findViewById(buttonId);
            selectedButton.toggle();
            Logger.i(" is checked: " + String.valueOf(selectedButton.isChecked()));
        }
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top