How to create a quiz app on android studio (using radio buttons)?

Dear readers.
Here we are going to learn about radio buttons.
Radio buttons -:
These are such type of buttons which are placed in a group but only one button is selected at a time. Example if we fill any form we often come across to gender section where we have to select only one from given options i.e. either male or female .

In app

tools used
1.EditText
2.TextView
3.Button
4.Radio Group
5.Radio  Button

      Layout :

android_main.xml file code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
    tools:context="com.example.ab.quiz.MainActivity"
    android:background="#22FF00FF">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:hint="Enter your name please"
        android:inputType="textPersonName"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/ques"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name"
        android:layout_alignStart="@+id/name"
        android:layout_below="@+id/name"
        android:layout_marginTop="21dp"
        android:text=""
        android:textSize="20sp"
        android:textStyle="bold" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:layout_below="@+id/ques"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/optionGroup"
        android:visibility="invisible">

        <RadioButton
            android:id="@+id/option1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ques"
            android:layout_marginLeft="50dp"
            android:text="" />

        <RadioButton
            android:id="@+id/option2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/option1"
            android:layout_marginLeft="200dp"
            android:text="" />

        <RadioButton
            android:id="@+id/option3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/option1"
            android:layout_alignLeft="@+id/option1"
            android:layout_marginLeft="50dp"
            android:text="" />

        <RadioButton
            android:id="@+id/option4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/option2"
            android:layout_alignLeft="@+id/option2"
            android:layout_marginLeft="200dp"
            android:text="" />
        </RadioGroup>

    <TextView
        android:id="@+id/response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_below="@+id/optionGroup"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="20dp" />

    <Button
        android:id="@+id/next"
        android:onClick="quiz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="Start"
        android:layout_below="@+id/response"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/score"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="33dp"
        android:layout_below="@+id/next"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

MainActivity,java file code 

package com.example.ab.quiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    EditText ed1;
    TextView tv1,tv2,tv3;
    RadioButton a,b,c,d;
    Button bt;
    RadioGroup rg;
    int q,s;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed1=(EditText)findViewById(R.id.name);
        tv1=(TextView)findViewById(R.id.ques);
        tv2=(TextView)findViewById(R.id.response);
        tv3=(TextView)findViewById(R.id.score);
        rg=(RadioGroup)findViewById(R.id.optionGroup);
        a=(RadioButton)findViewById(R.id.option1);
        b=(RadioButton)findViewById(R.id.option2);
        c=(RadioButton)findViewById(R.id.option3);
        d=(RadioButton)findViewById(R.id.option4);
        bt=(Button)findViewById(R.id.next);
        q=0;
        s=0;

    }
    public void quiz(View v){
        switch (q){
            case 0:
            {
                rg.setVisibility(View.VISIBLE);
                a.setChecked(false);
                b.setChecked(false);
                c.setChecked(false);
                d.setChecked(false);
                tv2.setText("");
                tv3.setText("");
                a.setEnabled(true);
                b.setEnabled(true);
                c.setEnabled(true);
                d.setEnabled(true);
                ed1.setEnabled(true);
                bt.setText("Next");
                s=0;

                tv1.setText("1.When did India its independece?");
                a.setText("1847");
                b.setText("1947");
                c.setText("1850");
                d.setText("1950");
                q=1;
                break;
            }
            case 1:
            {
                ed1.setEnabled(false);
                tv1.setText("2.Who is India's father of nation?");
                a.setText("Mahatma Gandhi");
                b.setText("Lal Bahadur Shashtri");
                c.setText("Jawaharlal Nehru");
                d.setText("Subhash Chandra Bose");

                if (b.isChecked()){
                    tv2.setText("Right Answer");
                    s=s+10;
                }
                else
                {
                    tv2.setText("Wrong Answer   B was Right Answer");
                }
                q=2;
                a.setChecked(false);
                b.setChecked(false);
                c.setChecked(false);
                d.setChecked(false);
                break;
            }
            case 2:
            {
                tv1.setText("3.Who was the first lady CM of Uttar Pradesh?");
                a.setText("Pratibha Singh Patil");
                b.setText("Indira Gandhi");
                c.setText("Sucheta Kriplani");
                d.setText("Mayawati");
                if (a.isChecked()){
                    s=s+10;
                    tv2.setText("Right Answer");

                }
                else
                {
                    tv2.setText("Wrong Answer   A was Right Answer");
                }
                q=3;
                a.setChecked(false);
                b.setChecked(false);
                c.setChecked(false);
                d.setChecked(false);
                break;
            }
            case 3:
            {

                tv1.setText("4.Who was first Indian lady to go in space?");
                a.setText("Mayawati");
                b.setText("Kalpana Chawla");
                c.setText("Kiran Bedi");
                d.setText("Sunita Williams");
                if (c.isChecked()){
                    s=s+10;
                    tv2.setText("Right Answer");
                }
                else
                {
                    tv2.setText("Wrong Answer  C was Right Answer");
                }
                q=4;
                a.setChecked(false);
                b.setChecked(false);
                c.setChecked(false);
                d.setChecked(false);
                break;
            }
            case 4:
            {
                tv1.setText("5.Who designed India's national flag?");
                a.setText("Rahul Gandhi");
                b.setText("Bankim Chandra Chatterjee");
                c.setText("Ishwar Chandra Vidyasagar");
                d.setText("Pingali Venkayya");
                if (b.isChecked()){
                    s=s+10;
                    tv2.setText("Right Answer");
                }
                else
                {
                    tv2.setText("Wrong Answer  B was Right Answer");
                }
                q=5;
                a.setChecked(false);
                b.setChecked(false);
                c.setChecked(false);
                d.setChecked(false);
                bt.setText("Finish");
                break;
            }
            case 5:
            {
                a.setEnabled(false);
                b.setEnabled(false);
                c.setEnabled(false);
                d.setEnabled(false);
                if (d.isChecked()){
                    s=s+10;
                    tv2.setText("Right Answer");
                }
                else
                {
                    tv2.setText("Wrong Answer  D was Right Answer");
                }
                tv3.setText(ed1.getText()+"'s final score is "+s);
                bt.setText("Restart");
                q=0;
                break;
            }
        }
    }
}

Output Screen shots :-

 

Leave a Reply