Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

RadioButtons in Android using Jetpack Compose

  • Last Updated : 17 Jun, 2021

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

Attributes of Radio Button

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. 

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. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle
import android.widget.Toast
import androidx.compose.material.*
import androidx.compose.material.Icon
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Phone
import androidx.compose.runtime.*
import androidx.compose.runtime.savedinstancestate.savedInstanceState
import androidx.compose.ui.Alignment
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.gfgapp.ui.GFGAppTheme
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.semantics.SemanticsProperties.ToggleableState
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.input.*
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        setContent {
            Column {
                // in below line we are calling
                  // a radio button  method.
@Preview(showBackground = true)
fun DefaultPreview() {
    GFGAppTheme {
fun SimpleRadioButtonComponent() {
    val radioOptions = listOf("DSA", "Java", "C++")
    val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[2]) }
        // 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 colums.
            radioOptions.forEach { text ->
                        // using modifier to add max 
                      // width to our radio button.
                        // below method is use to add
                      // selectable to our radio button.
                            // 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
                        // 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.
                            // 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,
                        modifier = Modifier.padding(start = 16.dp)



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




Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!