Android RadioButton And RadioGroup Tutorial

By | April 14, 2019

What is RadioButton ?

Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side.
only one radio button can be selected at a time.

What is RadioGroup ?

This class is used to create a multiple-exclusion scope for a set of radio buttons. Checking one radio button that belongs to a radio group unchecks any previously checked radio button within the same group.

RadioGroup is a Multiple RadioButton set on one RadioGroup.
Intially, all of the radio buttons are unchecked.

Implementation ClickListner in RadioButton

RadioButton define to click handler for a button,add to android:onClick attribute to the <RadioButton> element in xml layout.for this value must be the name of the method you want a call in response to click event.

Create New activity_main.xml file and following this code

Now another create class file for Following code :

Now i have created above code to define 3 RadioButton and In this RadioButon declared inside RadioGroup and another define TextView.

Now in this code to same name on click listener in attribute tag name on radiobutton. when user to click radiobutton to called this listener and display textview.

And in this code use to OnClickListener Method to called click event in radioButton. and main tain of if condition is declared boolean variable and check to radiobutton is selected or not ane then check if condition when condition is true then print the value of textview else not print.i have declared switch case statement and all case are check to inner if condition and then show the output.

Now another way to use without click can use RadioGroup click event on setOnCheckedChangeListener. it is called to radioGroup item. and when radioButton is checked or not then this listerner called.