Android RadioButton - Androhub

Radio Button

Android RadioButton

A RadioGroup class is used for set of radio buttons. If we check one radio button that belongs to a radio group, it automatically unchecks any previously checked radio button within the same group.

RadioGroup Attributes

Following are the important attributes related to RadioGroup control. You can check Android official documentation for complete list of attributes and related methods which you can use to change these attributes are run time.

AttributeDescription
android:checkedButtonThis is the id of child radio button that should be checked by default within this radio group.
android:backgroundThis is a drawable to use as the background.
android:contentDescriptionThis defines text that briefly describes content of the view.
android:idThis supplies an identifier name for this view.
android:onClickThis is the name of the method in this View's context to invoke when the view is clicked.
android:visibilityThis controls the initial visibility of the view.

Example

In this example i will be demonstrating how to control the Radio Buttons in Radio Group.

VIDEO DEMO

Let’s get start by creating a project in Eclipse IDE.

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)

2. Open your your AndroidManifest.xml file and make your “MainActivity” as Launcher activity as done in old projects.

3. Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml. In this layout i made two layouts separating two different types of radiobutton click event.

4. Now, add the following code in MainActivity.java. In this class we show the selected radio button name on button click and also show the selected radio button name on selecting radio button directly.

5. Run the application and you will get the output as shown in video and you are done.

Thanks. 🙂

 

Post comment

Your email address will not be published. Required fields are marked *