Android Get Value of Selected RadioButton

Mar 19, 2019
Check RadioButton based on value

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}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.