RadioButton and RadioGroup

Radio buttons are used where we want the user to select only one option out of many. But if we use radio buttons directly then each radio button will work independently and behaves like a checkbox. Hence, we must group the radio buttons using the radio group.

Here, in the following example, we need to do two things. One, increase the form length so that we can get enough space to design the form. Two we have to make the form scrollable by adding ScrollView to the form. As writing the details of how two things are done is a little difficult, I suggest that you watch the following video for a better understanding.

Step 1: Start an android project.

Step 2: Open strings.xml file and write the following code

<resources>
    <string name="app_name">RadioButton Demo</string>
    <string name="Title">Personality Check</string>
    <string name="Name">Name </string>
    <string name="email">E-mail ID </string>
    <string name="MoblieNo">Contact No </string>

    <string name="QNo1">1. Select Two Zero Two Four</string>
    <string name="Q1Op1">0044</string>
    <string name="Q1Op2">2044</string>
    <string name="Q1Op3">2024</string>
    <string name="Q1Op4">0024</string>

    <string name="QNo2">2. _______ I copied this program</string>
    <string name="Q2Op1">Yes</string>
    <string name="Q2Op2">No</string>
    <string name="Q2Op3">Ofcourse</string>
    <string name="Q2Op4">Definitely</string>

    <string name="QNo3">3. ________ , I am mad</string>
    <string name="Q3Op1">Yes</string>
    <string name="Q3Op2">No</string>
    <string name="Q3Op3">Maybe</string>
    <string name="Q3Op4">Ofcourse</string>

    <string name="btn_submit">Submit</string>
    <string name="Welcome2page">Submission successful, We will contact you soon either through mail or phone</string>


    <string name="btn_review">Review</string>
</resources>

Step 3: Design the first form as shown in the video. Add the second page to display a message “Form submitted successfully”.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="154dp"
        android:layout_marginLeft="154dp"
        android:layout_marginTop="36dp"
        android:layout_marginEnd="199dp"
        android:layout_marginRight="199dp"
        android:text="@string/Title"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="79dp"
        android:layout_height="35dp"
        android:layout_marginStart="42dp"
        android:layout_marginLeft="42dp"
        android:layout_marginTop="33dp"
        android:text="@string/Name"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="219dp"
        android:layout_height="57dp"
        android:layout_marginStart="55dp"
        android:layout_marginLeft="55dp"
        android:layout_marginTop="22dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView5"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="106dp"
        android:layout_height="40dp"
        android:layout_marginStart="42dp"
        android:layout_marginLeft="42dp"
        android:layout_marginTop="35dp"
        android:layout_marginEnd="28dp"
        android:layout_marginRight="28dp"
        android:text="@string/email"
        android:textSize="18sp"
        app:layout_constraintEnd_toStartOf="@+id/editText3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView5" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="28dp"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="22dp"
        android:layout_marginRight="22dp"
        android:ems="10"
        android:inputType="textEmailAddress"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView6"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="135dp"
        android:layout_height="35dp"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="33dp"
        android:text="@string/MoblieNo"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView6" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="18dp"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="28dp"
        android:layout_marginEnd="13dp"
        android:layout_marginRight="13dp"
        android:ems="10"
        android:inputType="phone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView7"
        app:layout_constraintTop_toBottomOf="@+id/editText3" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="53dp"
        android:layout_marginLeft="53dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="126dp"
        android:layout_marginRight="126dp"
        android:text="@string/QNo1"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText4" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="274dp"
        android:layout_height="156dp"
        android:layout_marginStart="44dp"
        android:layout_marginLeft="44dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="93dp"
        android:layout_marginRight="93dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2">

        <RadioButton
            android:id="@+id/radioButtonQ1Op1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Q1Op1" />

        <RadioButton
            android:id="@+id/radioButtonQ1Op2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Q1Op2" />

        <RadioButton
            android:id="@+id/radioButtonQ1Op3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Q1Op3" />

        <RadioButton
            android:id="@+id/radioButtonQ1Op4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Q1Op4" />
    </RadioGroup>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="48dp"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="57dp"
        android:layout_marginEnd="176dp"
        android:layout_marginRight="176dp"
        android:text="@string/QNo2"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.044"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup1" />

    <RadioGroup
        android:id="@+id/radioGroup2"
        android:layout_width="252dp"
        android:layout_height="150dp"
        android:layout_marginStart="51dp"
        android:layout_marginLeft="51dp"
        android:layout_marginTop="28dp"
        android:layout_marginEnd="108dp"
        android:layout_marginRight="108dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3">

        <RadioButton
            android:id="@+id/radioButtonQ2Op1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Q2Op1" />

        <RadioButton
            android:id="@+id/radioButtonQ2Op2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Q2Op2" />

        <RadioButton
            android:id="@+id/radioButtonQ2Op3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Q2Op3" />

        <RadioButton
            android:id="@+id/radioButtonQ2Op4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Q2Op4" />
    </RadioGroup>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="53dp"
        android:layout_marginLeft="53dp"
        android:layout_marginTop="68dp"
        android:layout_marginEnd="111dp"
        android:layout_marginRight="111dp"
        android:text="@string/QNo3"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup2" />

    <RadioGroup
        android:id="@+id/radioGroup3"
        android:layout_width="248dp"
        android:layout_height="146dp"
        android:layout_marginStart="55dp"
        android:layout_marginLeft="55dp"
        android:layout_marginTop="43dp"
        android:layout_marginEnd="93dp"
        android:layout_marginRight="93dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView4">

        <RadioButton
            android:id="@+id/radioButtonQ3Op1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Q3Op1" />

        <RadioButton
            android:id="@+id/radioButtonQ3Op2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Q3Op2" />

        <RadioButton
            android:id="@+id/radioButtonQ3Op3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Q3Op3" />

        <RadioButton
            android:id="@+id/radioButtonQ3Op4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Q3Op4" />

    </RadioGroup>

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="142dp"
        android:layout_height="62dp"
        android:layout_marginStart="51dp"
        android:layout_marginLeft="51dp"
        android:layout_marginTop="133dp"
        android:layout_marginEnd="87dp"
        android:layout_marginRight="87dp"
        android:layout_marginBottom="50dp"
        android:text="@string/btn_submit"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup3" />

</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

Step 4: Open MainActivity.java file and write the following code.

public class MainActivity extends AppCompatActivity {

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

        Button Submit=(Button)findViewById(R.id.btn_submit);
        Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent in= new Intent(MainActivity.this, SecondPage.class);
                startActivity(in);
            }
        });
    }
}

Step 5: Run the project