Search code examples
androidandroid-radiogroupandroid-radiobutton

Android RadioGroup get the RadioButton index


I created a RadioGroup programmatically and added in it some RadioButton (they can be 1 or 2 or 3, it dependes from the database). I tried this code but it crashes when I try to get the RadioButton selected ID

    final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroupTeamId);
    for (int i = 0; i < teamID.size(); i++)
    {
        RadioButton radioButton = new RadioButton(getActivity());
        radioButton.setText(i);
        if(i == 0)
            radioButton.setChecked(true);
        radioButton.setId(i);
        radioGroup.addView(radioButton);
    }
    radioGroup.invalidate();

    ........
    ........

    builder1.setPositiveButton("Ok", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int id)
        {
            int teamId = radioGroup..getCheckedRadioButtonId(); // Here the app crashes

            .........
        }
    });

XML file:

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

Log:

    07-23 18:49:40.920: E/AndroidRuntime(5437): FATAL EXCEPTION: main
    07-23 18:49:40.920: E/AndroidRuntime(5437): android.content.res.Resources$NotFoundException: String resource ID #0x0
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.content.res.Resources.getText(Resources.java:247)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.widget.TextView.setText(TextView.java:3473)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at com.example.apptesina.FragmentFindOperations.selectedItem(FragmentFindOperations.java:185)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at com.example.apptesina.FragmentFindOperations.access$1(FragmentFindOperations.java:154)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at com.example.apptesina.FragmentFindOperations$1.onItemClick(FragmentFindOperations.java:148)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.widget.AbsListView$1.run(AbsListView.java:3168)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.os.Handler.handleCallback(Handler.java:605)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.os.Handler.dispatchMessage(Handler.java:92)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.os.Looper.loop(Looper.java:137)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at android.app.ActivityThread.main(ActivityThread.java:4424)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at java.lang.reflect.Method.invoke(Method.java:511)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    07-23 18:49:40.920: E/AndroidRuntime(5437):     at dalvik.system.NativeStart.main(Native Method)

Solution

  • It is because you are adding radio button via code what getCheckedRadioButtonId does is look for radio buttons resource id, which can be done when you add Radio button in xml layout.

    See this

     <RadioGroup
        android:id="@+id/radioGroupTeamId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male" 
            android:checked="true" />
    
        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female" />
    
    </RadioGroup>
    

    Or Check this post

    You can manually keep track of the currently selected item or create a click listener on each radio button.