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

RadioButton in Kotlin

In Android development with Kotlin, a RadioButton is commonly used as part of the RadioGroup component to offer a set of mutually exclusive choices. Only one RadioButton can be selected in a RadioGroup at any given time.

Here's how you can use RadioButton in Kotlin for Android:

  • XML Layout:
<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>
  • Kotlin Code:
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 ->
            val radioButton: RadioButton = findViewById(checkedId)
            when (checkedId) {
                R.id.radioButton1 -> {
                    // Handle option 1
                }
                R.id.radioButton2 -> {
                    // Handle option 2
                }
                // Add more cases as required
            }
        }
    }
}

In this example:

  • The XML layout contains a RadioGroup with two RadioButton choices.
  • In the Kotlin code, the RadioGroup's setOnCheckedChangeListener listens for changes to the selected RadioButton. Inside the listener, you can determine which RadioButton was selected and take appropriate action.

You can also programmatically select a RadioButton by calling its setChecked(true) method, or find out if it's selected using its isChecked property. Remember, however, that setting one RadioButton in a RadioGroup will deselect the others.

  1. Android RadioButton example code in Kotlin:

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio Button" />
    
    val radioButton: RadioButton = findViewById(R.id.radioButton)
    
    // Set a click listener
    radioButton.setOnClickListener {
        // Handle radio button click
        if (radioButton.isChecked) {
            // Radio button is selected
        } else {
            // Radio button is deselected
        }
    }
    
  2. RadioButton group in Kotlin Android:

    Use a RadioGroup to group multiple RadioButtons:

    <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
            }
        }
    }
    
  3. Customizing RadioButton in Kotlin:

    Customize the appearance of RadioButton using XML attributes or programmatically in Kotlin.

    <RadioButton
        android:id="@+id/customRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Radio Button"
        android:buttonTint="@color/customColor" />
    
    val customRadioButton: RadioButton = findViewById(R.id.customRadioButton)
    
    // Programmatically change button color
    customRadioButton.buttonTintList = ColorStateList.valueOf(Color.RED)
    
  4. Dynamic creation of RadioButton in Kotlin:

    Programmatically create RadioButton instances and add them to a RadioGroup or a layout:

    val radioGroup: RadioGroup = findViewById(R.id.dynamicRadioGroup)
    
    val radioButton = RadioButton(this)
    radioButton.text = "Dynamic Radio Button"
    radioGroup.addView(radioButton)
    
  5. RadioButton with AlertDialog in Kotlin example:

    Use AlertDialog with a custom layout containing RadioButtons:

    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()