Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
pdiaz
16,073 PointsDoes anyone know how to use radiobuttons to start activities?
Does anyone know how to use radiobuttons to start activities?
pdiaz
16,073 PointsIm trying to move from one activity to another by checking a choice then submitting it through a button, this is the code I have so far.
''' public class MainActivity extends Activity {
RadioButton yes;
RadioButton no;
Button submitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yes = (RadioButton) findViewById(R.id.yesRadioButton);
no = (RadioButton) findViewById(R.id.noRadioButton);
submitButton = (Button) findViewById(R.id.submit);
if (yes.isChecked()) {
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent goToYes = new Intent(getApplicationContext(), YesActivity.class);
startActivityForResult(goToYes, 0);
}
});
} else if(no.isChecked()) {
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent goToNo = new Intent(getApplicationContext(), NoActivity.class);
startActivityForResult(goToNo, 0);
}
});
}
}
'''
1 Answer
Daniel Hartin
18,106 PointsHi Leonardo
In your code the code executes inside the onCreate() method so if neither of the radio buttons are checked (I am assuming the values are both set to false) then none of the code will operate and there is nothing that will repeat this check. In order to repeat the check I would move your if statement inside the onClick() method so each time the onClick() method is called the If statement is executed and the intent is created. See code for details. Please note I have not tried this so there may be some details need ironing out but it should give you the general idea.
Hope this helps
Daniel
RadioButton yes;
RadioButton no;
Button submitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yes = (RadioButton) findViewById(R.id.yesRadioButton);
no = (RadioButton) findViewById(R.id.noRadioButton);
submitButton = (Button) findViewById(R.id.submit);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (yes.isChecked()){
Intent goToYes = new Intent(getApplicationContext(), YesActivity.class);
startActivityForResult(goToYes, 0);
} else if(no.isChecked()) {
Intent goToNo = new Intent(getApplicationContext(), NoActivity.class);
startActivityForResult(goToNo, 0);
}
});
}
}
pdiaz
16,073 Pointsoh my mistake, I thought I saw my code inside the onclick method. I appreciate the help.
Gunjeet Hattar
14,483 PointsGunjeet Hattar
14,483 PointsHi Leonardo,
Can you add in a little more detail. Which activity are you referring here?