Layout
<RadioGroup
android:id="@+id/publishStatusRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/pendingRadioButton"
android:text="Pending"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/approvedRadioButton"
android:text="Approved"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rejectedRadioButton"
android:text="Rejected"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
Check RadioButton
based on value.
class Approval { companion object { const val APPROVED = 1 const val PENDING = 0 const val REJECTED = -1 }}
value = Approval.PENDINGval selected = when (value) { // Approval.PENDING -> R.id.pendingRadioButton Approval.APPROVED -> R.id.approvedRadioButton Approval.REJECTED -> R.id.rejectedRadioButton else -> R.id.pendingRadioButton}publishStatusRadioGroup.check(selected)
Get value based on checked RadioButton
.
val publishStatus = when(publishStatusRadioGroup.checkedRadioButtonId) { R.id.approvedRadioButton -> Approval.APPROVED R.id.rejectedRadioButton -> Approval.REJECTED else -> Approval.PENDING}