Android Programming Tutorials

(Romina) #1
A Fancier Form

case R.id.delivery:
r.setType("delivery");
break;
}
}
};
}

Recompile, reinstall, and run the application. Confirm that you can save the


restaurant data without errors.


If you are wondering what will happen if there is no selected RadioButton,


the RadioGroup call to getCheckedRadioButtonId() will return -1, which will


not match anything in our switch statement, and so the model will not be


modified.


Extra Credit.................................................................................................


Here are some things you can try beyond those step-by-step instructions:



  • If you have an Android device, try installing the app on the device


and running it there. The easiest way to do this is to shut down your
emulator, plug in your device, and run ant reinstall.


  • Set one of the three radio buttons to be selected by default, using


android:checked = "true".



  • Try creating the RadioButton widgets in Java code, instead of in the


layout. To do this, you will need to create the RadioButton objects
themselves, configure them (e.g., supply them with text to display),

then add them to the RadioGroup via addView().



  • Try adding more RadioButton widgets than there are room to display
    on the screen. Note how the screen does not automatically scroll to


show them. Then, wrap your entire layout in a ScrollView container,
and see how the form can now scroll to accommodate all of your

widgets.


26
Free download pdf