dave taddeo
dave taddeo

Reputation: 63

passing string from RadioButton android:text="@string/blue" to the next activity in android

i have

<RadioGroup
    android:id="@+id/sign_up_select_colour"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"

    <RadioButton
        android:id="@+id/rdbtn_red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/red" />

    <RadioButton
        android:id="@+id/rdbtn_blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/blue" />
</RadioGroup>

i want to send the string blue to the next activity the same way i did with EditText here (with your help earlier);

EditText editTextSignUpUserName = (EditText) findViewById(R.id.sign_up_user_name);
String signUpUserName = editTextSignUpUserName.getText().toString();
intent.putExtra("username", signUpUserName);

i've tried this;

RadioButton selectedRadioButton = (RadioButton)   findViewById(R.id.sign_up_select_colour);
String signUpColour = selectedRadioButton.getCheckedRadioButtonText().toString();
intent.putExtra("colour", signUpColour);

please and thanks.

Upvotes: 0

Views: 800

Answers (1)

Sam
Sam

Reputation: 86948

Ok, you're almost there. But a RadioGroup is not a RadioButton. Try this:

RadioGroup colourGroup = (RadioGroup) findViewById(R.id.sign_up_select_colour);
RadioButton button = (RadioButton) colourGroup.findViewById(colourGroup.getCheckedRadioButtonId());
String signUpColour = button.getText().toString();
intent.putExtra("gender", signUpColour);

Upvotes: 1

Related Questions