user1431282
user1431282

Reputation: 6835

Radio Groups in Android

I've been following the android documentation for RadioGroups (below for reference).

http://developer.android.com/guide/topics/ui/controls/radiobutton.html

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_pirates:
            if (checked)
                // Pirates are the best
            break;
        case R.id.radio_ninjas:
            if (checked)
                // Ninjas rule
            break;
    }
}

However, I noticed that in their example, each button is associated with an onRadioButtonClicked function. What if I don't want anything to be triggered on each click of the radio button but only when another Button is clicked?

The approach that I can think of is calling isChecked() on each radio button. However, this seems wordy. Is there a better way or more directed way of finding the id of the radio button that has been checked in a radio group in Android.

Upvotes: 1

Views: 104

Answers (1)

JafarKhQ
JafarKhQ

Reputation: 8734

Check RadioGroup.getCheckedRadioButtonId() to get Id of checked item.

Upvotes: 1

Related Questions