Android Radio Button

 

In this tutorial you will learn about android radio button & android radio group.

  • Android Radio Button widget provides interface to select item of your choice. You might be familiar with this, if you have worked on any of the scripting language.
  • Android Radio Group are used to Nest Android Radio Buttons together, means radio group controls selection of radio buttons which are placed inside it. This will allow only one Radio Button to get selected at any time. e.g. if we have criteria of gender than we are sure that a person can be either male or female but person cannot be both together. In this case we can take help of Radio Group to permit only one selection of gender by the user.

 


 

DownloadSource Code : RadioButton.zip


 

Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) RadioButton which you might have downloaded from above or you can follow the scripts below by copying it to implement in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/RadioButton/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have.

 

 

1. activity_main.xml : Layout file

 

Explaining above Code :

  • Line 12 We have declared a android radio group which is extended upto Line 42. Inside this radio group we have placed 3 android radio buttons namely radio_android, radio_ios, radio_windows. These 3 radio buttons will be controlled by this parent Radio Group, which means you will be able to select any one out of all three.
  • Line 16 We have made our radio group with android:orientation:”horizontal”. This is just to imply that user can select any one from the 3. You can also make it vertical.
  • Talking about radio button its declaration is quite simple. Further suppose if we want to make a particular radio button selected by default you can perform it  with property android:checked=”true” (refer Line-25 ).
  • After our TextView you can observe 2 more radio buttons. Since they are after Line 42 i.e. outside Radio Group, they will be independent for selection. You will be able to select both of them, without any restriction.

 

2. MainActivity.java

Explaining above Code :

  • Line 12 & 22-26 We have declared Radio Buttons, and linked them with Radio Button of (activity_main.xml)Layout file.
  • Line 35-51 deals with getting summary for all android radio button from handler method submit(). Over here we have placed Toast where we check whether android radio button are selected or not. radio_button_name.getText().toString() fetches string value of the radio button  & radio_button_name.isSelected() finds whether our radio is selected or not.
  • Line 48-49 If we  want to deselect a radio button programmatically then we can use syntax : radiobutton_Name.setChecked(false); Remove comment and see that when button is clicked if the radio button are checked they becomes unchecked. Its reverse is also possible if we wish to select our radio button then try radioButton_Name.setChecked(true); 

 

Final Output :


android radio button

 

Do It Yourself :

Try Checking our radio button by default from MainActivity.java file. For this simply inside onCreate() method add radiobutton_name.setChecked(true);

 

Report Errors + Bugs & Become Insider for Nestedif.com

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to insider@nestedif.com you could help other peers.