ShadowKnight
ShadowKnight

Reputation: 37

getCheckedradiobutton always returns -1

This is my code

final RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton rb1 = (RadioButton) findViewById(R.id.radio0);
RadioButton rb2 = (RadioButton) findViewById(R.id.radio1);  
RadioButton rb3 = (RadioButton) findViewById(R.id.radio2);
final CheckBox cb1 = (CheckBox) findViewById(R.id.chkbox1);
int id = rg1.getCheckedRadioButtonId();
System.out.println("------------------------|"+id);
switch (id) {
    case R.id.radio0:
        cb1.setEnabled(true);
        break;

    case R.id.radio1:
        cb1.setEnabled(true);
        break;

    case R.id.radio2:
        cb1.setEnabled(true);
        break;

    default:
        cb1.setEnabled(false);
        break;
    }

This always returns -1(chkbox always disabled) and i cant seem to make it work . plus ive also tried and assigned individual values through setId to the radiobutton and it doesnt work either.

This is my XML

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton1" 
        android:checkedButton ="1"        
        />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton2"
        android:checkedButton="2"              
        />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton3" 
        android:checkedButton="3"             
        />
</RadioGroup>

Upvotes: 2

Views: 548

Answers (2)

cyber_rookie
cyber_rookie

Reputation: 665

You are using the "android:checkedButton" property on the wrong view. This property identifies which radio button is a group is checked, while you're adding this property to each radio button. Try changing it to the following

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp" 
android:checkedButton="2"              
>

<RadioButton
    android:id="@+id/radio0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="RadioButton1" 
    />

<RadioButton
    android:id="@+id/radio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="RadioButton2"
    />

<RadioButton
    android:id="@+id/radio2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="RadioButton3" 
    />

Upvotes: 1

Neoh
Neoh

Reputation: 16174

Of course it won't work. You should use a listener to detect which radio button is checked, like this

rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        System.out.println("------------------------|"+checkedId);
        switch (checkedId) {
            ... 
        }
    }
});

Upvotes: 0

Related Questions