0

I want to know which RadioButton is checked for do an action.

But that doesn't work. In the log I have ID -1

mradioGroup = (RadioGroup) rootView.findViewById(R.id.choix_plateforme);
        mradioGroupt = (RadioButton) rootView.findViewById(R.id.ps4);
        mradioGroupa = (RadioButton) rootView.findViewById(R.id.xbox);
        mradioGroupw = (RadioButton) rootView.findViewById(R.id.pc);

        int selectedId = mradioGroup.getCheckedRadioButtonId();
        Log.d(getClass().getName(), "Plateforme_Choix = " + selectedId);
        // find which radioButton is checked by id

        if(mradioGroupt.isChecked()) {
            string_plateforme = "one";
        } else if(mradioGroupa.isChecked()) {
            string_plateforme = "two";;
        } else if (mradioGroupw.isChecked()){
            string_plateforme = "oops";
        }
        else {
            string_plateforme = "wrong";
        }

And my if statement doesn't work too.

<RadioGroup
    android:id="@+id/choix_plateforme"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/ps4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Playstation 4" />

    <RadioButton
        android:id="@+id/xbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="XBOX ONE" />

    <RadioButton
        android:id="@+id/pc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ordinateur" />

</RadioGroup>

Can you tell me where I was wrong? I didn't want new code, but please just explain to me my error if you can.

rgettman
  • 176,041
  • 30
  • 275
  • 357
CBJul
  • 31
  • 4
  • You start with no checked RadioButton. So you only will get a checked RadioButton if the user really taps on one of the RadioButtons. If you always want to have at least one checked RadioButton then you can set one of them checked either programmatically or via the attribute `android:checked="true"` – Bö macht Blau Apr 03 '18 at 18:03

1 Answers1

1

Try setting an OnCheckedChangeListener change listener:

        radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                switch(checkedId)
                {
                case R.id.ps4:
                     Log.d(getClass().getName(), "Plateforme_Choix =ps4");
                    break;
                case R.id.xbox:
                    Log.d(getClass().getName(), "Plateforme_Choix =xbox");
                    break;
                case R.id.pc:
                    Log.d(getClass().getName(), "Plateforme_Choix =pc");
                    break;
                }
            }
        });
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46