0

I have a radio group with the choices "Yes," "No," and "Maybe". I want a function that will return which choice the user picked.

Here is my code

 <RadioGroup
        android:id="@+id/answerRadioGroup"
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/submitButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/myTextView"
        app:layout_constraintVertical_bias="0.26">

        <RadioButton
            android:id="@+id/yesRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Yes" />

        <RadioButton
            android:id="@+id/noRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="No" />

        <RadioButton
            android:id="@+id/maybeRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Maybe" />

    </RadioGroup>

And here is the function I am trying to use:

private String checkGuess(){
        guessRadioGroup = (RadioGroup) findViewById(R.id.guessRadioGroup);
        int selectedId = guessRadioGroup.getCheckedRadioButtonId();
        radioButton = (RadioButton) findViewById(selectedId);
        String s = (String) radioButton.getText().toString();
        return s;
    }

This function seems to work when I replace String s = (String) radioButton.getText().toString(); with something simply like String s = "Whatever". The problem seems to be with using the getText() method.

This is what I use in MainActivity.java to display the text:

TextView tempTextView = (TextView) findViewById(R.id.tempTextView);
        tempTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String abc = checkGuess();
                tempTextView.setText("You chose: " + abc);
            }
        });
Joe
  • 3
  • 1
  • Possible duplicate of [Android getting value from selected radiobutton](https://stackoverflow.com/questions/18179124/android-getting-value-from-selected-radiobutton) – Swati May 16 '19 at 08:40
  • This is similar, and I tried using that way, but I'm not looking to put it into a Toast. I want to use a function to return the value of the item that I chose, so I can compare it later. – Joe May 16 '19 at 08:46

2 Answers2

0

You should use CustomListener for radioButton

radioButton.setOnCheckedChangeListener(new CustomListener());
class CustomListener implements   CompoundButton.OnCheckedChangeListener{

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch (buttonView.getId()){
                case R.id.rd1:

                    break;
            }
    }
}
0

You should use your radioGroup as parent for find radio button. And also you don't need cast as String while using toString().

private String checkGuess(){
    guessRadioGroup = (RadioGroup) findViewById(R.id.guessRadioGroup);
    int selectedId = guessRadioGroup.getCheckedRadioButtonId();
    radioButton = (RadioButton) guessRadioGroup.findViewById(selectedId);
    String s = radioButton.getText().toString();
    return s;
}

Hope it will help you.

Mohanraj
  • 296
  • 2
  • 13