How to get a value from radiogroup to another activity?

SmiLe ... picture SmiLe ... · Nov 5, 2012 · Viewed 13.1k times · Source

I want to make a new activity, like to confirm what radio-button is checked.

  • I have 2 radio-groups
  • Each group has 10 radio-buttons at least
  • I want to take whatever radio-button is checked value to the next activity

and for sure just one radio-button can be selected from every radio-group

Here is my xml code and I want the java code

  <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="391dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Where Are You" />

            <RadioGroup
                android:id="@+id/radioGroup0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollbarStyle="insideOverlay"
                android:scrollbars="vertical" >

                <RadioButton
                    android:id="@+id/radio0"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="it" />

                <RadioButton
                    android:id="@+id/radio1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Eng" />

                <RadioButton
                    android:id="@+id/radio2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="there" />
                <RadioButton
                    android:id="@+id/radio3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="it" />

                <RadioButton
                    android:id="@+id/radio4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Eng" />

                <RadioButton
                    android:id="@+id/radio5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="there" />
                <RadioButton
                    android:id="@+id/radio6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="it" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Where Do You Want To Go" />

            <RadioGroup
                android:id="@+id/radioGroup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollbarStyle="insideOverlay"
                android:scrollbars="vertical" >

                <RadioButton
                    android:id="@+id/radio12"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="it" />

                <RadioButton
                    android:id="@+id/radio13"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Eng" />

                <RadioButton
                    android:id="@+id/radio14"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="there" />
                <RadioButton
                    android:id="@+id/radio15"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="it" />

                <RadioButton
                    android:id="@+id/radio16"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Eng" />

                <RadioButton
                    android:id="@+id/radio17"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="there" />

            </RadioGroup>

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="OK" />

        </LinearLayout>
    </ScrollView>

I have deleted some of the radio-buttons so it doesn't get long

Please note I am a beginner.

Answer

Daniel Scocco picture Daniel Scocco · Nov 5, 2012

First, bind the RadioGroup:

RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);

Then get the position of the button that was checked:

int checked = rg.getCheckedRadioButtonId();

Then create the intent of your next activity:

Intent intent = new Intent(this,nextActivity.class);

Then put the information you want to pass on the intent:

intent.putExtra("checked",checked);

Then start the next activity:

startActivity(intent);

On the next activity you recover that info like this:

Intent intent = getIntent();
int checked = intent.getIntExtra("checked");