How to get a value from radiogroup to another activity?
13,879
Solution 1
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");
Solution 2
try that;
public void onCreate(Bundle savedInstanceState) {
enter code here
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
String selectedRadioValue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId() )).getText().toString();
RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup2);
String selectedRadioValue2 =((RadioButton)findViewById(rg.getCheckedRadioButtonId() )).getText().toString();
Button btn = (Button) findViewById(R.id.buttonName);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//enter code here for your control and
Intent intent = new Intent(getApplicationContext(), your.class);
intent.putExtra("radioGroup1Selected", selectedRadioValue);
intent.putExtra("radioGroup2Selected", selectedRadioValue2);
startActivity(intent);
}
});
}
get another activity :
Intent intent = getIntent();
String selectedRadioValue = intent.getStringExtra("selectedRadioValue");
String selectedRadioValue2 = intent.getStringExtra("selectedRadioValue2");
Author by
SmiLe ...
Updated on June 25, 2022Comments
-
SmiLe ... almost 2 years
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.