RadioButtons-AGN HUB

 RadioButton in Kotlin (Android)


Key Components

  1. RadioButton: The individual buttons representing the options.

  2. RadioGroup: Groups multiple RadioButton elements to enforce single selection.


Basic Implementation

XML Layout

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:padding="16dp">


    <RadioGroup

        android:id="@+id/radioGroup"

        android:layout_width="match_parent"

        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" />


        <RadioButton

            android:id="@+id/radioButton3"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Option 3" />

    </RadioGroup>


    <Button

        android:id="@+id/buttonSubmit"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Submit"

        android:layout_marginTop="16dp" />



</LinearLayout>

Kotlin Code

import android.os.Bundle

import android.widget.Button

import android.widget.RadioButton

import android.widget.RadioGroup

import android.widget.Toast

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)

        val buttonSubmit: Button = findViewById(R.id.buttonSubmit)


        buttonSubmit.setOnClickListener {

            // Get the selected RadioButton's ID

            val selectedId = radioGroup.checkedRadioButtonId

            if (selectedId != -1) {

                // Find the RadioButton by its ID

                val selectedRadioButton: RadioButton = findViewById(selectedId)

                val selectedText = selectedRadioButton.text

                Toast.makeText(this, "Selected: $selectedText", Toast.LENGTH_SHORT).show()

            } else {

                Toast.makeText(this, "No option selected", Toast.LENGTH_SHORT).show()

            }

        }

    }

}


Key Properties and Methods

  1. RadioButton:

    • isChecked: Boolean property to check if the button is selected.

    • text: Gets or sets the text of the button.

  2. RadioGroup:

    • checkedRadioButtonId: Returns the ID of the selected RadioButton. If none is selected, it returns -1.

    • setOnCheckedChangeListener: Listens for selection changes.


Advanced Features

Set Default Selection

You can set a default selected RadioButton programmatically:

radioGroup.check(R.id.radioButton2) // Sets "Option 2" as selected

Handle Change Events

Listen for changes in the selected RadioButton:

radioGroup.setOnCheckedChangeListener { group, checkedId ->

    val selectedRadioButton: RadioButton = findViewById(checkedId)

    Toast.makeText(this, "Selected: ${selectedRadioButton.text}", Toast.LENGTH_SHORT).show()

}

Deselect All RadioButtons

Clear the selection programmatically:

radioGroup.clearCheck()


Styling and Customization

  1. Custom Text Color:

  2. <RadioButton

  3.     android:layout_width="wrap_content"

  4.     android:layout_height="wrap_content"

  5.     android:text="Custom Option"

  6.     android:textColor="@color/black" />

  7. Custom Button Drawable: Replace the default radio button with a custom drawable:

  8. <RadioButton

  9.     android:layout_width="wrap_content"

  10.     android:layout_height="wrap_content"

  11.     android:button="@drawable/custom_radio_button" />

  12. Vertical or Horizontal Alignment: Use android:orientation in the RadioGroup to arrange buttons:

  13. <RadioGroup

  14.     android:layout_width="match_parent"

  15.     android:layout_height="wrap_content"

  16.     android:orientation="horizontal">

  17.     <!-- RadioButtons -->

  18. </RadioGroup>


Example: Dynamic RadioButtons

Kotlin Code

Create RadioButton elements programmatically:

val radioGroup: RadioGroup = findViewById(R.id.radioGroup)


// Add dynamic RadioButtons

val options = listOf("Dynamic Option 1", "Dynamic Option 2", "Dynamic Option 3")

for (option in options) {

    val radioButton = RadioButton(this).apply {

        text = option

        id = View.generateViewId() // Generate a unique ID

    }

    radioGroup.addView(radioButton)

}


Common Use Cases

  1. Forms: Single-choice questions like gender, subscription type, etc.

  2. Settings: Selecting preferences (e.g., dark mode or light mode).

  3. Filters: Choosing one filter category from a list.

Comments

Popular posts from this blog

Checkbox- AGN HUB

Timepicker- AGN HUB