The Wayback Machine - https://web.archive.org/web/20250118110202/https://www.geeksforgeeks.org/radiobuttons-in-android-using-jetpack-compose/
Open In App

RadioButtons in Android using Jetpack Compose

Last Updated : 22 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

RadioButtons are used to select only a specific option from the list of several options. Radio buttons are used in many places for selecting only one option from the list of two or more options. In this article, we will take a look at the implementation of this Radio button in Android using Jetpack Compose. 

Attributes of Radio Button

AttributeDescriptions
selectedthis attribute is used to make a radio button selected. 
onClickthis is used when our radio button is clicked. 
modifierto add padding to our radio buttons. 

enabled

used to make the RadioButton is enabled, allowing it to be clickable.

colors

used for customizing colors in RadioButton

interactionSource

uses a default MutableInteractionSource to handle interaction states.

Step-by-Step Implementation

Step 1: Create a New Project

To create a new project in the Android Studio Canary Version please refer to How to Create a new Project in Android Studio Canary Version with Jetpack Compose.

Step 2: Working with the MainActivity.kt file

Navigate to the app > java > your app’s package name and open the MainActivity.kt file. Inside that file add the below code to it.

Directory_Structure


Comments are added inside the code to understand the code in more detail.

Kotlin
package com.gfg.radiobutton_jetpack

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.selection.selectable
import androidx.compose.material3.RadioButton
import androidx.compose.material3.RadioButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.gfg.radiobutton_jetpack.ui.theme.RadioButton_JetpackTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Column {
                // in below line we are calling
                // the radio button.
                SimpleRadioButtonComponent()
            }
        }
    }
}

// @Preview function is use to see preview
// for our composable function in preview section.
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    RadioButton_JetpackTheme {
        // we are passing our composable
        // function to display its preview.
        SimpleRadioButtonComponent()
    }
}

@Composable
fun SimpleRadioButtonComponent() {
    val radioOptions = listOf("DSA", "Java", "C++")
    val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[2]) }
    Column(
        // we are using column to align our
        // radio buttons to center of the screen.
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight(),

        // below line is used for
        // specifying vertical arrangement.
        verticalArrangement = Arrangement.Center,

        // below line is used for
        // specifying horizontal arrangement.
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        // we are displaying all our
        // radio buttons in column.
        radioOptions.forEach { text ->
            Row(
                Modifier
                    // using modifier to add max
                    // width to our radio button.
                    .fillMaxWidth()

                    // below method is used to add
                    // selectable to our radio button.
                    .selectable(
                        // this method is called when
                        // radio button is selected.
                        selected = (text == selectedOption),

                        // below method is called on
                        // clicking of radio button.
                        onClick = { onOptionSelected(text) }
                    )

                    // below line is used to add
                    // padding to radio button.
                    .padding(horizontal = 16.dp),

                // below line is used for
                // specifying horizontal arrangement.
                horizontalArrangement = Arrangement.Start,

                // below line is used for
                // specifying vertical arrangement.
                verticalAlignment = Alignment.CenterVertically
            ) {
                // below line is used to get context.
                val context = LocalContext.current

                // below line is used to
                // generate radio button
                RadioButton(
                    // inside this method we are
                    // adding selected with a option.
                    selected = (text == selectedOption),

                    onClick = {
                        // inside on click method we are setting a
                        // selected option of our radio buttons.
                        onOptionSelected(text)

                        // after clicking a radio button
                        // we are displaying a toast message.
                        Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
                    },

                    // below line is used to Add padding around the RadioButton to separate it from other UI elements.
                    modifier = Modifier.padding(8.dp),

                    // below line is used to make the RadioButton is enabled, allowing it to be clickable.
                    enabled = true,

                    // below line is used for customizing colors in RadioButton
                    colors = RadioButtonDefaults.colors(
                        Color.Green,
                        Color.Blue
                    ),

                    // below line is uses a default MutableInteractionSource to handle interaction states.
                    interactionSource = remember { MutableInteractionSource() }
                )

                // below line is used to add
                // text to our radio buttons.
                Text(
                    text = text,
                    modifier = Modifier.padding(start = 16.dp),
                    fontWeight = FontWeight.Bold
                )
            }
        }
    }
}

 
 Now run your app and see the output of the app. 

Output:



 



Next Article

Similar Reads

three90RightbarBannerImg