0

I am trying to disable my RadioGroup so that the user cannot re-select and therefore my score count rises. I have seen an implementation here:

How to disable a RadioGroup until checkbox is checked

Albeit tried to go about it this way

My xml:

<RadioGroup
                android:id="@+id/rg1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/sm_margin"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/pates"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/sm_margin"
                    android:text="@string/pates"
                    android:onClick="onRadioButtonClicked" />

                <RadioButton
                    android:id="@+id/chips"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/sm_margin"
                    android:text="@string/chips"
                    android:onClick="onRadioButtonClicked" />

                <RadioButton
                    android:id="@+id/frites"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/sm_margin"
                    android:text="@string/frites"
                    android:onClick="onRadioButtonClicked" />

                <RadioButton
                    android:id="@+id/crepes"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/def_margin"
                    android:layout_marginLeft="@dimen/sm_margin"
                    android:text="@string/crepes"
                    android:onClick="onRadioButtonClicked" />

            </RadioGroup>
        </LinearLayout>

        <TextView
            android:id="@+id/question2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/def_margin"
            android:background="@drawable/layout_bg"
            android:fontFamily="@font/cambria"
            android:padding="@dimen/tv_padding"
            android:text="@string/question2" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="70dp"
            android:adjustViewBounds="true"
            android:src="@drawable/jc_fav" />

        <RadioGroup
            android:id="@+id/rg2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/sm_margin"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/mille_feuille"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/milleFeuille"
                android:onClick="onRadioButtonClicked" />

            <RadioButton
                android:id="@+id/creme_chantilly"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/cremeChantilly"
                android:onClick="onRadioButtonClicked" />

            <RadioButton
                android:id="@+id/opera"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/opera"
                android:onClick="onRadioButtonClicked" />

            <RadioButton
                android:id="@+id/creme_brulee"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/def_margin"
                android:text="@string/cremeBrulee"
                android:onClick="onRadioButtonClicked" />

        </RadioGroup>

Java:

int score = 0;
RadioGroup rgQ1;
RadioGroup rgQ2;

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

    rgQ1 = findViewById(R.id.rg1);
    rgQ2 = findViewById(R.id.rg2);
 }


public void onRadioButtonClicked(View view) {

    // Is the button now checked? then assign into a boolean named 'checked'
    boolean checked = ((RadioButton) view).isChecked();

    // Question 1 logic
    // Check correct answer is checked and update score

    switch (view.getId()) {
        case R.id.frites:
            if (checked) score += 1;
            Log.v("MainActivity", "score" + score);
            break;
    }

    // Disable RadioButtons of rg1
    for (int i = 0; i < rgQ1.getChildCount(); i++) {
        (rgQ1.getChildAt(i)).setEnabled(false);
    }

    // Question 2 logic
    // Check correct answer is checked and update score

    switch (view.getId()) {
        case R.id.mille_feuille:
            if (checked) score += 1;
            Log.v("MainActivity", "score" + score);
            break;
    }

    // Disable RadioButtons of rg2
    for (int i = 0; i < rgQ2.getChildCount(); i++) {
        (rgQ2.getChildAt(i)).setEnabled(false);
    }
}

}

It disables the first radiogroup when an item is selected and also the 2nd (before a user has selected for the 2nd radiogroup.

Any ideas?

fruitcakej
  • 31
  • 5

1 Answers1

0
You are disabling your second radio group in the same click event. Check which radio button group is selected. 

Like this, Similarly check group two and disable radio button group two!

int selectedId = radioGroup.getCheckedRadioButtonId();
if(selectedId != null){
//Then disable radio button group one
}
gorp88
  • 105
  • 14
  • not having much luck.. Using switch (view.getId()) { case R.id.frites: if (checked) score += 1; Log.v("MainActivity", "score" + score); break; } int selectedId = rgQ1.getCheckedRadioButtonId(); if(selectedId != null) { for (int i = 0; i < rgQ1.getChildCount(); i++) { (rgQ1.getChildAt(i)).setEnabled(false); } } i get an error of operator != cannot be applied to int, null – fruitcakej Jan 12 '18 at 21:17
  • integer.HasValue, use this in if case. Like if(selectedId.HasValue){ } – gorp88 Jan 12 '18 at 21:39