2

How to disable radio buttons group after clicking on some radio button in Android activity ?

This is my XML code

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tPitanje1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1. Neposredno regulisanje saobracaja na putevima vrse:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tPitanje1"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="62dp" >

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

            android:text="uniformisani komunalni policajci" />

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

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="inspektori za drumski saobracaj" />
    </RadioGroup>

</RelativeLayout>

And this is my Java Code

   package com.example.autoskola;

import android.app.Activity;
import android.os.Bundle;

public class obs1 extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.obs1);
    }

}

I'm interested in how to disable to change to click on another radiobutton?

eJoe
  • 485
  • 1
  • 7
  • 18
  • 1
    I'm a bit confused by your last sentence but it sounds like you want to disable a group after a user checks a button. If so, are you sure you want to do this? What if they accidentally pick the wrong one? – codeMagic Feb 23 '13 at 19:41

1 Answers1

4

If I'm understanding correctly, you want to disable the RadioGroup once a certain RadioButton is clicked. Just add an OnCheckedChangedListener and if the id of the RadioButton clicked is the right one, then disable the group by using setEnabled(false). Of course, you can't reenable the RadioGroup unless you have some other logic in your code that will do the re-enabling.

RadioGroup radioGroup = (RadioGroup)findViewById (R.id.radioGroup1);
radioGroup.setOnCheckedChangedListener (new OnCheckedChangedListener(){
public void onCheckedChanged(RadioGroup group, int checkedId) 
{
  if (checkedId == R.id.radio0)
    group.setEnabled(false);
}
});

Edit: It seems that setEnabled() doesn't work, so you should try changing your code so it loops through each RadioButton upon checking radio0:

if (checkedId == R.id.radio0){
   for(int i = 0; i < group.getChildCount(); i++){
            ((RadioButton)rg1.getChildAt(i)).setEnabled(false);
        }
}
Community
  • 1
  • 1
A--C
  • 36,351
  • 10
  • 106
  • 92
  • I'm sorry, when I looked at it was logical that the workbut when I tried to use this in my code showed that they can still change the value in RadioGroup. – eJoe Mar 01 '13 at 13:59
  • @Muskolo so you mean the RadioGroup is not being disabled? – A--C Mar 01 '13 at 14:10
  • Yes RadioGroup is not disabled it still can change RaddioButton i also try isClickable but no effects. – eJoe Mar 01 '13 at 14:37
  • 1
    @Muskolo Seems `setEnabled()` doesn't work, you should loop through the children like [this](http://stackoverflow.com/a/9377769/1815485) answer. – A--C Mar 01 '13 at 14:49