Sending radio button data to next activity

19,542
String str; // store the text corresponding to  the RadioButton which is clicked 

   switch(view.getId()) {
                case R.id.radioButton1:
                    if (checked)
                     str = "button1Text";
                        break;
                case R.id.radioButton2:
                    if (checked) str = "button2Text";
                        break;
                case R.id.radioButton3:
                    if (checked) str = "button3Text";
                        break;
         }

            Intent intent = new Intent(this, WinderDTActivity.class);
            intent.putExtra("radioChosen", str); // pass "str" to the next Activity

EDIT : To recieve the data in the next activity, use

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String message= extras.getString("radioChosen");

}
Share:
19,542
chakolatemilk
Author by

chakolatemilk

Computer Science student. Learning more from this website than I do at school. Thanks to everyone who has ever helped me in any problem I've had!

Updated on June 04, 2022

Comments

  • chakolatemilk
    chakolatemilk almost 2 years

    I'm trying to allow the user to enter their Name, and click on one of three radio buttons, and click on a submit button. And on the next activity, it will display their name and the radio button they selected. I've managed to be able to send the name, but I'm not sure how to send the radio button selection. Can someone help?

    This is what I have in the main activity layout .xml

    <EditText
        android:id="@+id/operatorName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:ems="10"
        android:hint="@string/operator_name" />
    
    <RadioGroup
        android:id="@+id/radioShifts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="10dp" >
        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio1"
            android:checked="true"
            android:onClick="onRadioButtonClicked" />
        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio2"
            android:onClick="onRadioButtonClicked" />
        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio3"
            android:onClick="onRadioButtonClicked" />
    </RadioGroup>
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:text="@string/button1"
        android:onClick="onButton1" />
    

    And I have this in the main_activity .java file:

        public final static String OP_NAME = "com.cyapps.downtimer.OPNAME";
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void onRadioButtonClicked(View view) {
            // Is the button now checked?
            boolean checked = ((RadioButton) view).isChecked();
            // Check which radio button was clicked
            switch(view.getId()) {
                case R.id.radioButton1:
                    if (checked)
                        break;
                case R.id.radioButton2:
                    if (checked)
                        break;
                case R.id.radioButton3:
                    if (checked)
                        break;
            }
    /*        Intent intent = new Intent(this, WinderDTActivity.class);
            EditText button = (EditText) findViewById(RadioGroup.getCheckedRadioButtonId());
            String radioChosen = button.getText().toString();
            intent.putExtra(RADIO_CHOSEN, radioChosen);*/
        }
    
        public void onButton1(View view) {
            Intent intent = new Intent(this, WinderDTActivity.class);
            EditText editText = (EditText) findViewById(R.id.operatorName);
            String opName = editText.getText().toString();
            intent.putExtra(OP_NAME, opName);
            startActivity(intent);
        }
    

    The code in /* */ is what I think I should do.. But I'm not sure. Someone help please? I'll really appreciate it..

  • chakolatemilk
    chakolatemilk over 11 years
    Okay, so I've done this after the switch: ` Intent intent2 = new Intent(this, WinderDTActivity.class); intent2.putExtra(RADIO_CHOSEN, str); startActivity(intent2);` but then on the next activity class, it says "Local variable str is not used.."
  • chakolatemilk
    chakolatemilk over 11 years
    Okay, nevermind, I see what I had done wrong in the code above. but now I put this in my next activity, and it still comes up blank. :( ` Intent intent = getIntent(); String opName = intent.getStringExtra(MainActivity.OP_NAME); String str = intent.getStringExtra(MainActivity.RADIO_CHOSEN); TextView textView = new TextView(this); textView.setTextSize(30); textView.setText(opName); textView.setText(str); setContentView(textView);`
  • Swayam
    Swayam over 11 years
    Sorry for the inconvenience. I have updated my code. It should work now.
  • chakolatemilk
    chakolatemilk over 11 years
    It says string message is not used. And couldn't I have left intent.putExtra(RADIO_CHOSEN, str); the way it was?
  • Swayam
    Swayam over 11 years
    Yeah you can let RADIO_CHOSEN be as it was. But just make sure that it is the same in both the activities.
  • Swayam
    Swayam over 11 years
    And 'String message is not used` is a warning which is coming because you have declared the variable and you might not be using it else where in the code.
  • chakolatemilk
    chakolatemilk over 11 years
    Yes, I see that, did you mean to put String str instead then? Because I just copied it from yours, and that's what it said. :(
  • Swayam
    Swayam over 11 years
    Where exactly are you getting the error ? in the first activity or in the second activity ?
  • chakolatemilk
    chakolatemilk over 11 years
    Sorry, I fixed the error. But now, when I do this for the second activity.java: ` Bundle extras = getIntent().getExtras(); if (extras != null){ TextView textView = new TextView(this); String opName = extras.getString(MainActivity.OP_NAME); String str = extras.getString(MainActivity.RADIO_CHOSEN); textView.setTextSize(30); textView.setText(opName); textView.setText(str); setContentView(textView); }` I only see the radio button string when I run it, I don't see the name AND the radio button string.
  • Swayam
    Swayam over 11 years
    Try this : String opName = extras.getString(MainActivity.OP_NAME); String str = extras.getString(MainActivity.RADIO_CHOSEN); String myString = opName + "/" + str; textView.setText(mySTring);
  • Swayam
    Swayam over 11 years
    Get both the strings and then just concatenate them into a single string and display it.
  • chakolatemilk
    chakolatemilk over 11 years
    Hi! Sorry for not responding or anything, I had midterms this week, but thanks for everything. The last thing you mentioned worked! Well, I did it somewhat differently, but thank you so much for the ideas and helping me get started. I really appreciate it.
  • Swayam
    Swayam over 11 years
    It's alright man! I am glad that you finally got it working. Good luck ahead! Happy coding. Cheers! :)