RadioButtons in Android using Jetpack Compose

04 Mar, 2022
RadioButtons are used to select only a specific option from the list of several options in a list or either. 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. 
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. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.graphics.drawable.shapes.Shapeimport android.media.Imageimport android.os.Bundleimport android.widget.Toastimport androidx.appcompat.app.AppCompatActivityimport androidx.compose.foundation.*import androidx.compose.foundation.Textimport androidx.compose.foundation.layout.*import androidx.compose.foundation.selection.selectableimport androidx.compose.foundation.shape.CircleShapeimport androidx.compose.foundation.shape.RoundedCornerShapeimport androidx.compose.foundation.text.KeyboardOptionsimport androidx.compose.material.*import androidx.compose.material.Iconimport androidx.compose.material.icons.Iconsimport androidx.compose.material.icons.filled.AccountCircleimport androidx.compose.material.icons.filled.Infoimport androidx.compose.material.icons.filled.Menuimport androidx.compose.material.icons.filled.Phoneimport androidx.compose.runtime.*import androidx.compose.runtime.savedinstancestate.savedInstanceStateimport androidx.compose.ui.Alignmentimport androidx.compose.ui.layout.ContentScaleimport androidx.compose.ui.platform.setContentimport androidx.compose.ui.res.imageResourceimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport com.example.gfgapp.ui.GFGAppThemeimport androidx.compose.ui.Modifierimport androidx.compose.ui.draw.clipimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.graphics.SolidColorimport androidx.compose.ui.platform.ContextAmbientimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.res.colorResourceimport androidx.compose.ui.semantics.SemanticsProperties.ToggleableStateimport androidx.compose.ui.text.TextStyleimport androidx.compose.ui.text.font.FontFamilyimport androidx.compose.ui.text.input.*import androidx.compose.ui.unit.Dpimport androidx.compose.ui.unit.TextUnit class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContent {            Column {                // in below line we are calling                  // a radio button  method.                SimpleRadioButtonComponent()            }        }    }} @Preview(showBackground = true)@Composablefun DefaultPreview() {    GFGAppTheme {        SimpleRadioButtonComponent();    }} @Composablefun SimpleRadioButtonComponent() {    val radioOptions = listOf("DSA", "Java", "C++")    val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[2]) }    Column(        // we are using column to align our           // imageview 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.        Column {            // below line is use to set data to            // each radio button in columns.            radioOptions.forEach { text ->                Row(                    Modifier                        // using modifier to add max                       // width to our radio button.                        .fillMaxWidth()                        // below method is use 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 use to add                       // padding to radio button.                        .padding(horizontal = 16.dp)                ) {                    // below line is use to get context.                    val context = ContextAmbient.current                                       // below line is use to                    // generate radio button                    RadioButton(                        // inside this method we are                          // adding selected with a option.                        selected = (text == selectedOption),modifier = Modifier.padding(all = Dp(value = 8F)),                        onClick = {                            // inide 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_LONG).show()                        }                    )                    // below line is use to add                    // text to our radio buttons.                    Text(                        text = text,                        modifier = Modifier.padding(start = 16.dp)                    )                }            }        }    }}
  
Now run your app and see the output of the app. 
 
 
surinderdawra388
sweetyty
Android-Jetpack
Technical Scripter 2020
Android
Kotlin
Technical Scripter
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
Android SDK and it's Components
Flutter - Custom Bottom Navigation Bar
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android UI Layouts
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example