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);
}
});