Tommy
Tommy

Reputation: 759

Radio buttons on click listenener

I have radio buttons in an application I am creating. When the user clicks on one option, I want the other radio buttons to be unclickable.

I'm not sure how to do this, the radio buttons are set in an onclick listener and the logic is set within on click.

Upvotes: 0

Views: 709

Answers (2)

apesa
apesa

Reputation: 12443

If your using XML you can group them using RadioGroup, in the following example only one of the radio buttons is selectable at a time.

    <RadioGroup
        android:id="@+id/rg_configup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        >
        <RadioButton
            android:id="@+id/rb_configup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sales"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            >
        </RadioButton>
        <RadioButton
            android:id="@+id/rb_configup2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Regional"
            android:layout_above="@+id/rb_configup1"
            android:layout_alignLeft="@+id/rb_configup1"
            >
        </RadioButton>
        <RadioButton
            android:id="@+id/rb_configup3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Global"
            android:layout_above="@+id/rb_configup2"
            android:layout_alignLeft="@+id/rb_configup1"
            >
        </RadioButton>
        <RadioButton
            android:id="@+id/rb_configup4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ad-hoc"
            android:layout_above="@+id/rb_configup3"
            android:layout_alignLeft="@+id/rb_configup1"
            >
        </RadioButton>
    </RadioGroup>

Upvotes: 0

Cristian
Cristian

Reputation: 200160

What about calling setEnabled(false) on the rest of the buttons? If there are too many buttons you can consider putting in an array:

RadioButton[] buttons; //you put here all buttons..

// then, when you catch the click, call a method like this
private void disableOtherButtons(RadioButton buttonClicked)
    for(RadioButton button : buttons){
        if( button != buttonClicked ){
            button.setEnabled(false);
        }
    }
}

Upvotes: 3

Related Questions