Radio button dynamically

The Radio Button in android can also be created programmatically or dynamically, other than creating it through drag and drop from the palette. With a single click, we can convert an unchecked radio button to a checked radio button.

 It also implements the setOnCheckedChangeListener() method of the RadioGroup class.

Example of Dynamic RadioButton:

activity_main.xml: (File: activity_main.xml)

<?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” xmlns:app=”http://schemas.android.com/apk/res-auto” android:id=”@+id/container” android:layout_width=”match_parent” android:layout_height=”match_parent” tools:context=”.MainActivity” android:orientation=”vertical”>