Bloat RadioGroup in Android-multiple radioButtons at a time

I made a RadioGroup with a variable number of radioButtons. The problem is that many radio objects can be checked, this is not one RadioButton checked in 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> 

range hood / 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 radio group has strange functionality. If I check the first RadioGroup, and then the second, the first on unchecked ... after that, if I want to select the first again, I can not do this ... is not checked anymore. Can someone help me? Any idea is welcome! Thanks in advance.

+4
source share
2 answers

I found a solution: the problem was that all buttons have the same identifier. Therefore, I set a unique identifier for each radioButton.

+7
source

I will give an example of how this is done, then you modify your code accordingly. In your xml, to add the switches, you need to define the layout as shown 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 the following: i) Declare a radio group and a radio object, as shown below -

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

ii) Register your radio unit and radio group using the xml layout as shown below -

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

iii) Add onclicklistner to the button as shown below -

 btn.setOnClickListner(this); 

iv) Implement the OnClickListner interface and override the unrealized methods of the OnClickListner interface

v) In the overridden OnClick method, enter 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.

-2
source

Source: https://habr.com/ru/post/1491801/


All Articles