0

So I have created a CRUD type of app where users fill in textfields and select options from a radio button and it must appear on the next page in a list.

I was able to validate textfields that users have entered and save them except the radio buttons. I need assistance with when the user selects option in the radio button that it must save and show in the list as the other ones.

This is my Insert data page enter image description here

I want when I click create button, the user selected option must be saved and be shown in the next page I created. enter image description here

How can I achieve this with using radio buttons in android, thanks.

This is my code below how I declare the radio buttons.

rdgrp = view.findViewById(R.id.RadioGroup01);
        malebutton = view.findViewById(R.id.malebutton);
        femalebutton = view.findViewById(R.id.femalebutton);

On button click:

String strRadio = rdgrp.toString();
//where I display
strRadio.setText("");I get an error here
SaDev
  • 145
  • 1
  • 11
  • Does this answer your question? [Android getting value from selected radiobutton](https://stackoverflow.com/questions/18179124/android-getting-value-from-selected-radiobutton) – Nitish Jan 06 '23 at 12:08

1 Answers1

0

Add Radio Buttons in RadioGroup.

<RadioGroup
    android:id="@+id/groupradio"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radio_male"
        android:text="Male"/>

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radio_female"
        android:text="Female"/>

</RadioGroup>

On submit form,

RadioGroup radioGroup = findViewById(R.id.radioGroup);
int selectedId = radioGroup.getCheckedRadioButtonId();
if (selectedId == -1) {
    Toast.makeText(MainActivity.this, "No answer has been selected", Toast.LENGTH_SHORT).show();
} else {
    RadioButton radioButton = radioGroup.findViewById(selectedId);
    String gender = String.valueOf(radioButton.getText());
}
Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23
  • Thanks @SohaibAhmed, so how I then show it in my setText, for example if I say gender.setText(""), it gives me error that gender was not declared. Even when I add it to my DB, it refuses like this: collegediaryTable.insertRecord(gender); – SaDev Jan 06 '23 at 12:39
  • if you need to settext, you can do `textview.setText(gender);` – Sohaib Ahmed Jan 06 '23 at 14:06