Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
When working with the traditional Android View system (not Jetpack Compose), RadioGroup
is the container for RadioButton
views. It ensures that only one RadioButton
can be selected at a time.
Here's a step-by-step guide to using RadioGroup
in Kotlin for Android:
RadioGroup
and a few RadioButton
views:<!-- res/layout/activity_main.xml --> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" /> <!-- Add more RadioButtons as required --> </RadioGroup>
OnCheckedChangeListener
on the RadioGroup
to listen for changes to the selected RadioButton
:import android.os.Bundle import android.widget.RadioButton import android.widget.RadioGroup import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val radioGroup: RadioGroup = findViewById(R.id.radioGroup) radioGroup.setOnCheckedChangeListener { group, checkedId -> // Find the RadioButton that was selected by its ID val radioButton: RadioButton = findViewById(checkedId) when (checkedId) { R.id.radioButton1 -> { // Handle option 1 } R.id.radioButton2 -> { // Handle option 2 } // Add more cases as required } } } }
With the code above, you can detect which RadioButton
was selected and then perform an action accordingly.
Remember, RadioGroup
ensures that only one RadioButton
can be selected at a time. If you programmatically check one RadioButton
by calling its setChecked(true)
method, any previously selected button in the same group will automatically be unchecked.
Android RadioGroup example code in Kotlin:
<RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" /> </RadioGroup>
val radioGroup: RadioGroup = findViewById(R.id.radioGroup) // Set a listener for radio group changes radioGroup.setOnCheckedChangeListener { group, checkedId -> // Handle radio group change when (checkedId) { R.id.radioButton1 -> { // Option 1 selected } R.id.radioButton2 -> { // Option 2 selected } } }
Customizing RadioGroup in Kotlin:
Customize the appearance of the RadioGroup
using XML attributes or programmatically in Kotlin.
<RadioGroup android:id="@+id/customRadioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/custom_background">
val customRadioGroup: RadioGroup = findViewById(R.id.customRadioGroup) // Programmatically customize RadioGroup customRadioGroup.setBackgroundResource(R.drawable.custom_background)
Dynamic creation of RadioGroup in Kotlin:
Programmatically create a RadioGroup
instance and add it to a layout:
val dynamicRadioGroup = RadioGroup(this) dynamicRadioGroup.layoutParams = ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT ) parentLayout.addView(dynamicRadioGroup)
RadioGroup with AlertDialog in Kotlin example:
Use AlertDialog
with a custom layout containing a RadioGroup
:
val options = arrayOf("Option 1", "Option 2", "Option 3") AlertDialog.Builder(this) .setTitle("Select an option") .setSingleChoiceItems(options, -1) { dialog, which -> // Handle selection dialog.dismiss() } .create() .show()
Kotlin RadioGroup with multiple columns:
Use GridLayout
as the parent layout for the RadioGroup
to achieve a multi-column layout:
<GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="2"> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="start" android:layout_column="0"> <!-- RadioButtons go here --> </RadioGroup> <!-- Add another RadioGroup for the second column --> </GridLayout>