I was doing the Android Developer Fundamentals Course when I had the following doubt in the below code.
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.sameday:
if (checked)
// Same day service
displayToast(getString(R.string.same_day_messenger_service));
break;
case R.id.nextday:
if (checked)
// Next day delivery
displayToast(getString(R.string.next_day_ground_delivery));
break;
case R.id.pickup:
if (checked)
// Pick up
displayToast(getString(R.string.pick_up));
break;
default:
// Do nothing.
break;
}
}
My doubt is we could just have used the view variable to understand which radiobutton was clicked since view.getId() returns the id of the selected radio button. At the moment the code uses isChecked() and view.getId() both.