{"id":3025,"date":"2020-04-12T13:21:01","date_gmt":"2020-04-12T03:21:01","guid":{"rendered":"https:\/\/learntodroid.com\/?p=3025"},"modified":"2020-04-12T13:21:11","modified_gmt":"2020-04-12T03:21:11","slug":"android-radio-button-and-radio-group-tutorial-with-examples","status":"publish","type":"post","link":"http:\/\/10.0.0.14:32769\/android-radio-button-and-radio-group-tutorial-with-examples\/","title":{"rendered":"Android Radio Button and Radio Group Tutorial with Examples"},"content":{"rendered":"\n
RadioButton and RadioGroup are widgets used in Android development for when the user needs to capture a single option from a set of options that are all displayed at the once in the user interface.<\/p>\n\n\n\n
For each option that user can select from a RadioButton widget needs to be added to the layout. The RadioGroup is used to group the related RadioButtons together to ensure only one RadioButton in the RadioGroup can be selected at any one time.<\/p>\n\n\n\n
Common use cases for the Android RadioButton and RadioGroup widget include:<\/p>\n\n\n\n
I have created a tutorial on how to use the RadioButton and RadioGroup widgets in Android which includes examples with sample code.<\/p>\n\n\n\n
In this tutorial on RadioButton and RadioGroup widgets I will cover the following topics:<\/p>\n\n\n\n
All of the code samples shared in this tutorial are available in the RadioButtonTutorial GitHub repository I created at the following link.<\/p>\n\n\n\n
https:\/\/github.com\/learntodroid\/RadioButtonTutorial<\/a><\/p>\n\n\n\n In this section of the post I have documented some of the common XML attributes and methods used for the RadioButton and RadioGroup widgets.<\/p>\n\n\n\n To get access to all of the XML attributes, methods and constructors available for these widgets I would recommend looking at the following links to the official documentation.<\/p>\n\n\n\n In the first section of this tutorial for showing how to use radio buttons in Android we will be creating a simple multiple choice quiz app that will look like the screenshot below.<\/p>\n\n\n\n In the app the user will be able to answer a question by selecting one of the answers shown as radio buttons. When the user selects the “Submit Answer” button, if the selected radio button is the correct answer a Toast with a message saying it is the correct answer will be shown. If it is not correct an incorrect answer Toast message will be displayed.<\/p>\n\n\n\nRadioButton & RadioGroup Attributes and Methods<\/h2>\n\n\n\n
RadioButton<\/h3>\n\n\n\n
RadioButton Attributes<\/h4>\n\n\n\n
No.<\/th> Attribute<\/th> Description<\/th><\/tr><\/thead> 1<\/td> android:id<\/td> Identifier for the RadioButton, allows the View for the RadioButton to be located using the findViewById(int) method<\/td><\/tr> 2<\/td> android:text<\/td> Text displayed in the label of the RadioButton<\/td><\/tr> 3<\/td> android:onClick<\/td> Method name that is called when the user clicks on the RadioButton<\/td><\/tr> 4<\/td> android:checked<\/td> If set to “true” the RadioButton will show by default as checked, if set to “false” the RadioButton will show by default as unchecked<\/td><\/tr> 5<\/td> android:button<\/td> Change the appearance of the button by setting a drawable to the RadioButton<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n RadioButton Methods<\/h4>\n\n\n\n
No.<\/th> Method<\/th> Description<\/th><\/tr><\/thead> 1<\/td> CharSequence getText()<\/td> Retrieve the text associated to the label of the RadioButton shown on screen<\/td><\/tr> 2<\/td> void setText(CharSequence)<\/td> Set the text of the label of the RadioButton to a new value<\/td><\/tr> 3<\/td> boolean isChecked()<\/td> Returns true if the RadioButton is checked, returns false if the RadioButton is not checked<\/td><\/tr> 4<\/td> void setChecked(boolean)<\/td> Sets the RadioButton to checked if the parameter passed is true, sets the RadioButton to unchecked if the parameter passed is false<\/td><\/tr> 5<\/td> void toggle()<\/td> Will set the RadioButton to checked if the RadioButton is currently unchecked, and will set the RadioButton to unchecked if the RadioButton is currently checked<\/td><\/tr> 6<\/td> void setOnClickListener(View.OnClickListener)<\/td> Registers a callback to be run when the RadioButton is clicked<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n RadioGroup<\/h3>\n\n\n\n
RadioGroup Attributes<\/h4>\n\n\n\n
No.<\/th> Attribute<\/th> Description<\/th><\/tr><\/thead> 1<\/td> android:id<\/td> Identifier for the RadioGroup, allows the View for the RadioGroup to be located using the findViewById(int) method<\/td><\/tr> 2<\/td> android:checkedButton<\/td> The id of the RadioButton within the RadioGroup to load as checked by default<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n RadioGroup Methods<\/h4>\n\n\n\n
No.<\/th> Method<\/th> Description<\/th><\/tr><\/thead> 1<\/td> int getCheckedRadioButtonId()<\/td> Retrieve the id of the RadioButton within the RadioGroup that is checked<\/td><\/tr> 2<\/td> void check(int)<\/td> Use the id to locate the RadioButton within the RadioGroup and set it to checked<\/td><\/tr> 3<\/td> void clearCheck()<\/td> Sets all RadioButtons under the RadioGroup to unchecked<\/td><\/tr> 4<\/td> int getChildCount()<\/td> The RadioGroup is a ViewGroup, this method will return the number of children of the RadioGroup<\/td><\/tr> 5<\/td> View getChildAt(int)<\/td> The RadioGroup is a ViewGroup, this method will return the View of the child at the index of the RadioGroup passed as a parameter<\/td><\/tr> 6<\/td> void setOnCheckedChangeListener
(RadioGroup.OnCheckedChangeListener)<\/td>Registers a callback to be run when the RadioButton within the RadioGroup is checked or unchecked<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n How to Use the RadioButton and RadioGroup Widgets in Android<\/h2>\n\n\n\n