RadioButtons-AGN HUB
RadioButton in Kotlin (Android)
Key Components
RadioButton: The individual buttons representing the options.
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
RadioButton:
isChecked: Boolean property to check if the button is selected.
text: Gets or sets the text of the button.
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
Custom Text Color:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Option"
android:textColor="@color/black" />
Custom Button Drawable: Replace the default radio button with a custom drawable:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/custom_radio_button" />
Vertical or Horizontal Alignment: Use android:orientation in the RadioGroup to arrange buttons:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- RadioButtons -->
</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
Forms: Single-choice questions like gender, subscription type, etc.
Settings: Selecting preferences (e.g., dark mode or light mode).
Filters: Choosing one filter category from a list.
Comments
Post a Comment