0

Is there any possible way to get the selected radio button in this layout? because rg.getCheckedRadioButtonId() not working on this layout. I can't get each of my radiobuttons ID. It's like all my radio button are out of my radio Group

  <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rg"
        android:gravity="center"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <RadioButton
                android:id="@+id/rad1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"/>

            <RadioButton
                android:id="@+id/rad2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"    />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <RadioButton
                android:id="@+id/rad3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"/>

            <RadioButton
                android:id="@+id/rad4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"/>
        </LinearLayout>
    </RadioGroup>

so what i did is like this, but I can only get the ID of one radiobutton. How can I get all ID of my radiobutton and get what is selected?.

                       bt.setOnClickListener(new View.OnClickListener() {
                                  @Override
                                  public void onClick(View v) {

                                      int radioButtonId = rg.getCheckedRadioButtonId();
                                      if(rg.getCheckedRadioButtonId()!= -1){

                                          RadioButton selectedans = (RadioButton) findViewById(radioButtonId);
                                          String selectedansText = selectedans.getText().toString();

                                          if (selectedansText == answer[position]) {

                                               //SelectedansText match with answer

                                          }
                                          if(selectedansText != answer[position]) {
                                                //selectedansText not match with answer
                                          }
                                          rg.clearCheck();

                                          if (position < question.length) {
                                              tv.setText(question[position]);
                                              r1.setText(opts[position * 4]);
                                              r2.setText(opts[position * 4 + 1]);
                                              r3.setText(opts[position * 4 + 2]);
                                              r4.setText(opts[position * 4 + 3]);
                                          } else {

                                              Intent intent = new Intent(grade_four_post_test.this, grade_four_post_results.class);
                                              startActivity(intent);

                                          }
                                      }
                                      else
                                      {
                                          Toast.makeText(grade_four_post_test.this, "Choose Answer",
                                                  Toast.LENGTH_SHORT).show();

                                      }
                                  }
                              }
        );
Drenyl
  • 906
  • 1
  • 18
  • 41
  • you can use `setOnCheckedChangeListener` for all radio buttons, then check in code, or use two `radioGroup` inside `LinearLayout`. – Shayan Pourvatan Jan 29 '16 at 13:07
  • i'll try but i don't think it will work, it was like all the `radiobuttons` are out of the `radiogroup` . i use this xml code to make customize position of radiobtns – Drenyl Jan 29 '16 at 13:21
  • check this answer http://stackoverflow.com/a/35239621/2826147 @Lynerd – Amit Vaghela Feb 06 '16 at 10:00

2 Answers2

1

Check this answer i have done in my studio, hope this helps,

    public class StackOne extends AppCompatActivity {

    private RadioButton rb1, rb2, rb3, rb4;
    private RadioGroup rg;
    private String selectedType = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stack_one);

        rb1 = (RadioButton) findViewById(R.id.rad1);
        rb2 = (RadioButton) findViewById(R.id.rad2);
        rb3 = (RadioButton) findViewById(R.id.rad3);
        rb4 = (RadioButton) findViewById(R.id.rad4);
        rg = (RadioGroup) findViewById(R.id.rg);

        GetSelectedRadioButton();
    }

    private void GetSelectedRadioButton() {

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (group.getCheckedRadioButtonId()) {
                    case R.id.rad1:
                        selectedType = "rButton1";
                        rb1.setChecked(true);
                        Toast.makeText(StackOne.this,"Radiobutton 1 checked",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.rad2:
                        selectedType = "rButton2";
                        rb2.setChecked(true);
                        Toast.makeText(StackOne.this,"Radiobutton 2 checked",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.rad3:
                        selectedType = "rButton2";
                        rb3.setChecked(true);
                        Toast.makeText(StackOne.this,"Radiobutton 3 checked",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.rad4:
                        selectedType = "rButton2";
                        rb4.setChecked(true);
                        Toast.makeText(StackOne.this,"Radiobutton 4 checked",Toast.LENGTH_LONG).show();
                        break;

                }
            }
        });
    }
}

Checkout this for more detail.

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • tell me if facing any issue @Lynerd – Amit Vaghela Feb 08 '16 at 10:10
  • i will try this sorry late reply – Drenyl Feb 08 '16 at 11:33
  • @Lynerd , this is the perfect solution and will surely work. – Amit Vaghela Feb 08 '16 at 11:35
  • i can't change my code easily i almost done but this is still my problem, can you check my code at the top i edited it, thanks, i'm getting a liittle bit confused – Drenyl Feb 08 '16 at 11:49
  • I need to get the ID of a selected `radiobutton` in my `radiogroup` and get its string using `selectedansText` and compare it on my `String Array` `answer[position]` – Drenyl Feb 08 '16 at 11:53
  • you have to write setOnCheckedChangeListener event for it, as written in my answer and maintain flag selectedType for that from that you can get selected radiobutton and than compare that selectedType string to string array @Lynerd – Amit Vaghela Feb 08 '16 at 12:18
0

You can get selected radio button by this way

int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);

this will return you the id of selected radio button

Mobile Apps Expert
  • 1,028
  • 1
  • 8
  • 11
  • in my layout, this code is not working, i can't get the id of each radiobtn using `radioGroup.getCheckedRadioButtonId();` , so i need to use other method and i don't know how, tnx btw – Drenyl Jan 29 '16 at 13:15