Skip to content Skip to sidebar Skip to footer

Sending Radio Button Data To Next Activity

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 th

Solution 1:

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

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

}

Solution 2:

- Use switch to get the Radio button selected.

switch (types.getCheckedRadioButtonId()) {
      case R.id.sit_down:
        intent.putExtra(SIT_DOWN, "sitdown");
        break;
      case R.id.take_out:
        intent.putExtra(TAKE_OUT, "takeout");
        break;

      }

- Now just do the startActivity(intent);

Post a Comment for "Sending Radio Button Data To Next Activity"