I am trying to make a simple form activity in order to learn how to use the various components supplied by Eclipse. I was doing great until I got to the radio buttons.
The application allows a user to fill out an entire form and then click the send button. When the send button is clicked I create a new intent, pass all of the form data into the intent and start up a new activity to act as a sort of confirmation/summary page.
How do I go about retrieving which radiobutton the user has clicked? I have five in total.
Below is the code from my onCreate method.
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.feedback_scroll);
//References to XML
name = (EditText)findViewById(R.id.nameEntryBox);
county = (Spinner)findViewById(R.id.countySpinner);
//Set array adapter
county.setAdapter(fillCountySpinner());
submitBtn = (Button)findViewById(R.id.submitFormBtn);
atmo1 = (RadioButton)findViewById(R.id.arad1);
atmo2 = (RadioButton)findViewById(R.id.arad2);
atmo3 = (RadioButton)findViewById(R.id.arad3);
atmo4 = (RadioButton)findViewById(R.id.arad4);
atmo5 = (RadioButton)findViewById(R.id.arad5);
ser1 = (RadioButton)findViewById(R.id.srad1);
ser2 = (RadioButton)findViewById(R.id.srad2);
ser3 = (RadioButton)findViewById(R.id.srad3);
ser4 = (RadioButton)findViewById(R.id.srad4);
ser5 = (RadioButton)findViewById(R.id.srad5);
rating = (RatingBar)findViewById(R.id.ratingBar1);
//Add a listener to the submit button - Anonymous inner class
submitBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Create a new Intent
Intent i = new Intent(v.getContext(), FeedbackResults.class);
choice = (String)county.getSelectedItem();
//Add extra parameters
//Name
i.putExtra("name", name.getText());
//County
i.putExtra("county", choice);
//Dob
//Atmosphere
i.putExtra("atmosphere", atmos);
//Service
i.putExtra("service", serv);
//Rating
i.putExtra("rating", rating.getRating());
//Start new Activity that will display a review of the customers feedback
startActivity(i);
//Toast message
Toast.makeText(v.getContext(), "Thank you for your feedback!", Toast.LENGTH_SHORT).show();
}
});
}
I was looking at another topic relating to my question on this site. They said to use a switch statement. I understand the logic with it and wrote in the methods to do this, but I don’t know how to pass the View variable to it..What is this view variable relating to?? THis is the method I wrote.
public void onRadioButtonClicked1(View v){
switch(v.getId()){
case R.id.arad1:
atmos = "1";
break;
case R.id.arad2:
atmos = "2";
break;
case R.id.arad3:
atmos = "3";
break;
case R.id.arad4:
atmos = "4";
break;
case R.id.arad5:
atmos = "5";
break;
}
}
Any feedback is very much appreciated!
Each group of radio buttons should be in a group, I assume you did this already in your XML file.
Assuming that, then you get the ID of the checked radio button by doing:
You can do that anywhere in an
Activity, or if you’re using aFragment, then you just need to put agetView()in it:So I would change your
onClick: