-4

I am creating a quiz app where in it asks a question and gives users options.For options I have used RadioButton So what I want is that once the user clicked any one of the options it should not allow any other options to be clicked but the problem is that it can be still clicked.if possible please write the code.Thanks!

private int Question_no=0;
private Boolean Boolean_Var=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question1);

    String[] Question_Array = getResources().getStringArray(R.array.Question1);
    TextView Questions = (TextView) findViewById(R.id.Question);
    Questions.setText(Question_Array[Question_no]);

    String[] Radio_Button1_Array = getResources().getStringArray(R.array.Option_1);
    RadioButton Radio_Button1 = (RadioButton) findViewById(R.id.radioButton1);
    Radio_Button1.setText(Radio_Button1_Array[Question_no]);

    String[] Radio_Button2_Array = getResources().getStringArray(R.array.Option_2);
    RadioButton Radio_Button2 = (RadioButton) findViewById(R.id.radioButton2);
    Radio_Button2.setText(Radio_Button2_Array[Question_no]);

    findViewById(R.id.MenuButton).setVisibility(View.INVISIBLE);
    findViewById(R.id.NextButton).setVisibility(View.INVISIBLE);
}

public void On_RadioButton1_Click (View view)
{
    if (Boolean_Var == false) {
        String[] CorrectAns_Array = getResources().getStringArray(R.array.Answer1);
        String CorrectAns = CorrectAns_Array[Question_no];
        String[] Answer_Array = getResources().getStringArray(R.array.Option_1);
        String Answer = Answer_Array[Question_no];


        if (Answer.equals(CorrectAns)) {
            RadioButton Right_Ans = (RadioButton) findViewById(R.id.radioButton1);
            Right_Ans.setTextColor(Color.GREEN);
            AnswerSubmitted();
        } else {
            RadioButton Wrong_Ans = (RadioButton) findViewById(R.id.radioButton1);
            Wrong_Ans.setTextColor(Color.RED);;
            GreenTick();
            AnswerSubmitted();
        }
    }
    Boolean_Var = true;

}

public void On_RadioButton2_Click(View view)
{
    if(Boolean_Var==false)
    {
        String[] CorrectAns_Array = getResources().getStringArray(R.array.Answer1);
        String CorrectAns = CorrectAns_Array[Question_no];
        String[] Answer_Array = getResources().getStringArray(R.array.Option_2);
        String Answer = Answer_Array[Question_no];


        if(Answer.equals(CorrectAns))
        {
            RadioButton Right_Ans = (RadioButton) findViewById(R.id.radioButton2);
            Right_Ans.setTextColor(Color.GREEN);
        }
        else
        {
            RadioButton Wrong_Ans = (RadioButton) findViewById(R.id.radioButton2);
            Wrong_Ans.setTextColor(Color.RED);
            GreenTick();
        }
    }
    Boolean_Var=true;
    AnswerSubmitted();
}

 public void GreenTick()
{
    String[] Answer1_Array = getResources().getStringArray(R.array.Option_1);
    String Answer1 = Answer1_Array[Question_no];

    String[] Answer2_Array = getResources().getStringArray(R.array.Option_2);
    String Answer2 = Answer2_Array[Question_no];

    String[] Answer3_Array = getResources().getStringArray(R.array.Option_3);
    String Answer3 = Answer3_Array[Question_no];

    String[] The_CorrectAns_Array = getResources().getStringArray(R.array.Answer1);
    String The_CorrectAnsIs = The_CorrectAns_Array[Question_no];

    if(The_CorrectAnsIs.equals(Answer1))
    {
        Button Option_with_CorrectAns = (Button) findViewById(R.id.radioButton1);
        Option_with_CorrectAns.setTextColor(Color.GREEN);
    }
    else if(The_CorrectAnsIs.equals(Answer2))
    {
        Button Option_with_CorrectAns = (Button) findViewById(R.id.radioButton2);
        Option_with_CorrectAns.setTextColor(Color.GREEN);
    }
    else if(The_CorrectAnsIs.equals(Answer3))
    {
        Button Option_with_CorrectAns = (Button) findViewById(R.id.radioButton3);
        Option_with_CorrectAns.setTextColor(Color.GREEN);
    }
    else
    {
        Button Option_with_CorrectAns = (Button) findViewById(R.id.radioButton4);
        Option_with_CorrectAns.setTextColor(Color.GREEN);
    }
}

 public void AnswerSubmitted()
{
    findViewById(R.id.MenuButton).setVisibility(View.VISIBLE);
    findViewById(R.id.NextButton).setVisibility(View.VISIBLE);
}
}
alok dubey
  • 13
  • 1
  • 4

3 Answers3

5

try this after click on radiobutton :

   yourradioButtonId.setClickable(false);
Bapusaheb Shinde
  • 839
  • 2
  • 13
  • 16
1

You can use setClickable(false);.For this:

yourradiobutton.setClickable(false);
Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
Omar Dhanish
  • 885
  • 7
  • 18
0

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);
    }
}
karthik
  • 347
  • 1
  • 9
  • FYI, if you setEnabled(false) the radio button it will be grayed out and won't be visible as "checked". It doesn't only disable the click, but disables the whole radio button (so no more "checked" state) – M. Wojcik Dec 12 '19 at 10:23