-3

I created an MCQs App with four Radio Buttons in a Radio Group. Problem is that I want to select one radio button and disable other three radio button selection at that time. Only one Radio button selects and disable other radio button selection. Kindly help me out: I will be very grateful to all of you.

----------

Xml File

 <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/radioGroup1">
//These are four radio buttons from which i have to select one radio button and selection of other three radio buttons must be disabled.
        <RadioButton
            android:id="@+id/choice1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:background="@android:color/darker_gray"
            android:gravity="left|center_vertical"
            android:onClick="onClick"
            android:padding="4dp"
            android:text="A"
            android:textColor="#000000"
            android:textSize="16dp" />

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="B"
        android:onClick="onClick"
        android:background="@android:color/darker_gray"
        android:textColor="#000000"
        android:padding="4dp"
        android:textSize="16dp"
        android:gravity="left|center_vertical"
        android:layout_marginBottom="5dp"
        android:id="@+id/choice2"/>

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="C"
        android:onClick="onClick"
        android:background="@android:color/darker_gray"
        android:textColor="#000000"
        android:padding="4dp"
        android:textSize="16dp"
        android:gravity="left|center_vertical"
        android:layout_marginBottom="5dp"
        android:id="@+id/choice3"/>

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="D"
        android:onClick="onClick"
        android:background="@android:color/darker_gray"
        android:textColor="#000000"
        android:padding="4dp"
        android:textSize="16dp"
        android:gravity="left|center_vertical"
        android:layout_marginBottom="5dp"
        android:id="@+id/choice4" />
</RadioGroup>

----------

Java File:
 mQuestionView = (TextView) findViewById(R.id.question);
        mButtonChoice1 = (RadioButton) findViewById(R.id.choice1);
        mButtonChoice2 = (RadioButton) findViewById(R.id.choice2);
        mButtonChoice3 = (RadioButton) findViewById(R.id.choice3);
        mButtonChoice4 = (RadioButton) findViewById(R.id.choice4);
 }
  //Java file with only onclick button code:
 public void onClick(View view) {
            Button answer = (Button) view;
            // Is the button now checked?
            //here must be the code of my problem
        }

2 Answers2

0

For Disabling RadioGroup try the code Below. If it does not work try looping.

RadioGroup radioGroup = (RadioGroup)findViewById (R.id.radioGroup1);
radioGroup.setOnCheckedChangedListener (new OnCheckedChangedListener(){
public void onCheckedChanged(RadioGroup group, int checkedId) 
{
  if (checkedId == R.id.choice1)
    group.setEnabled(false);
}else if(/*other conditions here*/)
});

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

if (checkedId == R.id.choice1){

   //radio0 is checked save the data

   for(int i = 0; i < group.getChildCount(); i++){
            ((RadioButton)rg1.getChildAt(i)).setEnabled(false);
        }
}else if(/*other conditions here*/){

       for(int i = 0; i < group.getChildCount(); i++){
            ((RadioButton)rg1.getChildAt(i)).setEnabled(false);
        }
 }
Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28
0

Im not quite sure what you are asking for, but i think that is a solution:

public void onClick(View view)
{
    ArrayList<Integer> ids = new ArrayList<Integer>();
    ids.add(mButtonChoice1.getId());
    ids.add(mButtonChoice2.getId());
    ids.add(mButtonChoice3.getId());
    ids.add(mButtonChoice4.getId());
    for(Integer i : ids)
    {
        if(i!= view.getId())
            ((RadioButton) findViewById(i)).setEnabled(false);
    }
}
irdkwmnsb
  • 303
  • 2
  • 9
  • It works but when i click on next question, the radio buttons will not reset, it keep its changings from previous mcq question. – Android Learner 541 Dec 02 '17 at 03:50
  • what to do ? with it – Android Learner 541 Dec 02 '17 at 10:32
  • Well, you should add these lines to your event accuring on next question: `ArrayList ids = new ArrayList(); ids.add(mButtonChoice1.getId()); ids.add(mButtonChoice2.getId()); ids.add(mButtonChoice3.getId()); ids.add(mButtonChoice4.getId()); for(Integer i : ids) { ((RadioButton) findViewById(i)).setEnabled(true); }` – irdkwmnsb Dec 06 '17 at 05:33