Inflating RadioGroup in Android-multiple radioButtons check at a time
Asked Answered
S

2

5

I made a RadioGroup with a variable number of radioButtons. The problem is that many radiobuttons can be checked, it is not a single RadioButton checked in a RadioGroup (as I know) Where is my mistake? Here is my code :

View view = inflater.inflate(R.layout.questionnaire_check_question, null);
RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
    for (int k = 0; k < answerValues.size(); k++) {
         View radioButtonView = inflater.inflate(R.layout.check_radio_button, null);
         RadioButton radioButton = (RadioButton) radioButtonView
                            .findViewById(R.id.radio_button);
         radioButton.setText(answerValues.get(k));
         radioGroup.addView(radioButtonView);
    }
questionBody.addView(view);

questionnaire_check_question.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:background="#A4A7A6" >

    <RadioGroup
        android:id="@+id/radioGroup"
        android:scrollbars="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </RadioGroup>

</LinearLayout>

check_radio_button.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radio_button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_marginLeft="5dp"
    android:button="@drawable/checkbox"
 >

</RadioButton>

drawable/checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/icon_checkbox_on" android:state_checked="true"/>
    <item android:drawable="@drawable/icon_checkbox_off" android:state_checked="false"/>

</selector>

The radioGroup has a strange functionality..if I check the first RadioGroup, and then the second one, the first on become unchecked...after this, if I want to select again the first one, I can't do this..it doesn't check anymore. Can anyone help me ? Any idea is welcome! Thanks in advance.

Smitten answered 17/7, 2013 at 7:39 Comment(0)
S
11

I found the solution: the problem was that all buttons have the same ID. So I set a unique id for each radioButton.

Smitten answered 17/7, 2013 at 8:43 Comment(0)
A
-2

I am giving an example how this is done, you then modify your code accordingly. In your xml, to add radio buttons, you need to define a layout like below -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male" 
            android:checked="true" />

        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female" />

    </RadioGroup>

    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />

</LinearLayout>

In your Java File, do this: i)Declare radio group and radiobutton like below -

 private RadioGroup radioSexGroup;
  private RadioButton radioSexButton;
  private Button btn;

ii)Register your radiobutton and radiogroup with the xml layout like below -

radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);

iii)Add onclicklistner on button like below -

btn.setOnClickListner(this);

iv)Implement OnClickListner interface and Override the unimplemented methods of OnClickListner interface

v) In the overrided method OnClick, code the following -

int selectedId = radioSexGroup.getCheckedRadioButtonId();

            // find the radiobutton by returned id
                radioSexButton = (RadioButton) findViewById(selectedId);

            Toast.makeText(MyAndroidAppActivity.this,
                radioSexButton.getText(), Toast.LENGTH_SHORT).show();

And this is how it is done..Hope this helps.

Andria answered 17/7, 2013 at 8:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.