-1

I am working on a quiz app. There are 5 questions in the app. For each question there are 4 options. For each question i am using a radio group. So far i am just able to display the toast message for every correct answer when the correct answer radio button is checked. I want to display the quiz results in a toast message after the submit button is clicked. I have mention the points in my java code which i want to display. They are in the submitQuiz method. Below, i am providing my code for the app.

XML code:

<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="match_parent"
    tools:context="com.infinitystone.mani.quiz.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:gravity="center_horizontal"
            android:text="General Knowledge Quiz"
            android:textAllCaps="true"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="italic" />

        <EditText
            android:id="@+id/name_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="8dp"
            android:hint="Enter Your Name"
            android:inputType="textCapWords" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:text="Question 1:"
            android:textAllCaps="true"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="8dp"
            android:text="@string/question_1"
            android:textColor="@android:color/background_dark" />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/radio_answer_1a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_1a" />

            <RadioButton
                android:id="@+id/radio_answer_1b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_1b" />

            <RadioButton
                android:id="@+id/radio_answer_1c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_1c" />

            <RadioButton
                android:id="@+id/radio_answer_1d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_1d" />
        </RadioGroup>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="152dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/black">

        </View>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:text="Question 2:"
            android:textAllCaps="true"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="8dp"
            android:text="@string/question_2"
            android:textColor="@android:color/background_dark" />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/radio_answer_2a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_2a" />

            <RadioButton
                android:id="@+id/radio_answer_2b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_2b" />

            <RadioButton
                android:id="@+id/radio_answer_2c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_2c" />

            <RadioButton
                android:id="@+id/radio_answer_2d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_2d" />
        </RadioGroup>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="152dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/black">

        </View>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:text="Question 3:"
            android:textAllCaps="true"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="8dp"
            android:text="@string/question_3"
            android:textColor="@android:color/background_dark" />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/radio_answer_3a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_3a" />

            <RadioButton
                android:id="@+id/radio_answer_3b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_3b" />

            <RadioButton
                android:id="@+id/radio_answer_3c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_3c" />

            <RadioButton
                android:id="@+id/radio_answer_3d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_3d" />
        </RadioGroup>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="152dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/black">

        </View>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:text="Question 4:"
            android:textAllCaps="true"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="8dp"
            android:text="@string/question_4"
            android:textColor="@android:color/background_dark" />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/radio_answer_4a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_4a" />

            <RadioButton
                android:id="@+id/radio_answer_4b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_4b" />

            <RadioButton
                android:id="@+id/radio_answer_4c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_4c" />

            <RadioButton
                android:id="@+id/radio_answer_4d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_4d" />
        </RadioGroup>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="152dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/black">

        </View>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:text="Question 5:"
            android:textAllCaps="true"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="8dp"
            android:text="@string/question_5"
            android:textColor="@android:color/background_dark" />

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/radio_answer_5a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_5a" />

            <RadioButton
                android:id="@+id/radio_answer_5b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_5b" />

            <RadioButton
                android:id="@+id/radio_answer_5c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_5c" />

            <RadioButton
                android:id="@+id/radio_answer_5d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="radioGroup"
                android:paddingLeft="8dp"
                android:text="@string/answer_5d" />
        </RadioGroup>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="152dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/black">

        </View>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:orientation="horizontal">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:onClick="submitQuiz"
                android:text="Submit"
                android:textAllCaps="true" />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="16dp"
                android:layout_weight="1"
                android:onClick="resetQuiz"
                android:text="Reset"
                android:textAllCaps="true" />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

Java code:

package com.infinitystone.mani.quiz;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    // This method is called when the radio button with correct answer is checked
    public void radioGroup(View view) {
        boolean checked = ((RadioButton) view).isChecked();
        switch (view.getId()) {

            case R.id.radio_answer_1b:
                if (checked) {
                    Toast.makeText(this, "Correct answer",
                            Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.radio_answer_2a:
                if (checked) {
                    Toast.makeText(this, "Correct answer",
                            Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.radio_answer_3b:
                if (checked) {
                    Toast.makeText(this, "Correct answer",
                            Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.radio_answer_4a:
                if (checked) {
                    Toast.makeText(this, "Correct answer",
                            Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.radio_answer_5d:
                if (checked) {
                    Toast.makeText(this, "Correct answer",
                            Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    // This method is called when the submit button is clicked
    public void submitQuiz(View view){
      /*
      * I want to display the following details when the submit button is clicked
      *
      * Number of questions attempted
      * Number of right answers
      * Number of wrong answers
      *
      * All this summary in a toast message
      * */
    }

}

String.xml file:

<resources>
    <string name="app_name">Quiz</string>

    <string name="question_1">Hitler party which came into power in 1933 is known as ?</string>
    <string name="answer_1a">Labour Party</string>
    <string name="answer_1b">Nazi Party</string>
    <string name="answer_1c">Ku-Klux-Klan</string>
    <string name="answer_1d">Democratic Party</string>
    <!-- Correct answer: Nazi party -->

    <string name="question_2">First human heart transplant operation conducted by Dr. Christiaan Barnard on Louis Washkansky, was conducted in which year ?</string>
    <string name="answer_2a">1967</string>
    <string name="answer_2b">1968</string>
    <string name="answer_2c">1958</string>
    <string name="answer_2d">1922</string>
    <!-- Correct answer: 1967 -->

    <string name="question_3">In which year Georgia, Uzbekistan and Turkmenistan became the members of UN ?</string>
    <string name="answer_3a">1991</string>
    <string name="answer_3b">1992</string>
    <string name="answer_3c">1993</string>
    <string name="answer_3d">1994</string>
    <!-- Correct answer: 1992 -->

    <string name="question_4">Escape velocity of a rocket fired from the earth towards the moon is a velocity to get rid of what ?</string>
    <string name="answer_4a">Earth gravitational pull</string>
    <string name="answer_4b">Moon gravitational pull</string>
    <string name="answer_4c">Centripetal force due to the earth rotation</string>
    <string name="answer_4d">Pressure of the atmosphere</string>
    <!-- Correct answer: Earth gravitational pull  -->

    <string name="question_5">Which instrument is used for seeing objects at the surface of water from a submarine under water ?</string>
    <string name="answer_5a">Kaleidoscope</string>
    <string name="answer_5b">Spectroscope</string>
    <string name="answer_5c">Telescope</string>
    <string name="answer_5d">Periscope</string>
    <!-- Correct answer: Periscope  -->
</resources>
mani
  • 27
  • 8

1 Answers1

1

Since you said you can display the Toast message when checking the buttons, I assume you know how to define and find views in code.

What you need to do now is to declare a Button variable and find view for it. You may do this in your onCreate() method:

btn_submit = (Button) findViewByID(R.id.btn_submit)

where btn_submit is a global variable you should define in your activity, since you need to have access to it in other methods. BTW, you didn't specify ids for Buttons in your R.layout.activity_main.xml, which you should (I assume it to be btn_sumbit, and you should change the find view code according to your naming).

Now what you need to do is to listen for the button click actions. So after finding view for the Button, you need to give it a onClickListener. You may do this in the following way:

btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  submitQuiz();
            }
});

Now the button will listen for the click event, and it will call your submitQuiz() method when clicked.

Also, you should declare global variables and find views for each of the RadioGroups. So that you can use them in your submitQuiz() method. Now you can use the following code to do the grading logic:

switch ( rg_question1.getCheckedRadioButtonId() ) {
                        case R.id.answer_1a:
                            // wrong
                            break;
                        case R.id.rb_answer_1b:
                            // correct
                            correctCount++;
                            correctQustionList.add("qustion1");
                            break;
                        case ...
}

At last, show the toast message. I believe you know how to do this part.

  • Do i have to assign an id for the submit button and each radio group also ? – mani Jun 16 '17 at 06:45
  • Yes. Else you wouldn't know which button was clicked – wick.ed Jun 16 '17 at 15:52
  • I didn't understand your code properly.. Can you please give me some more detailed code – mani Jun 16 '17 at 17:34
  • You have to assign IDs to each view that you want to reference in code. In your case, Buttons and RadioGroups and RadioButtons have to have IDs, otherwise how would your device know which view you are reffering to? I used my preferred naming style for the views, which might be confusing for you, my apologies. In the above code, btn_xxx stands for Button, rg_xxx stands for RadioGroup, rb_xxx stands for RadioButton etc. So btn_submit is a variable that I want to refer to the submit Button in the layout file. – CristianoYL Jun 16 '17 at 17:50
  • Basically, you need to 1) declare View variables, such as Button, RadioGroup. 2) find reference for these variables, i.e. btn_submit = (Button) findViewByID(R.id.btn_submit) 3) Register Listener and Callback for the Buttons. i.e. btn_submit.setOnClickListener(...). And that's it. – CristianoYL Jun 16 '17 at 17:50
  • Also, you didn't provide your full code in the question, so I'm a bit confused how would your code work. It's always better to provide some more information about your current code and problem, otherwise I wouldn't know which part you got confused. I thought my code was clear enough if you have a basic concept on how to find the views and use them, since you claim you can show the toast message when checking the RadioButtons. If not, please let me know. – CristianoYL Jun 16 '17 at 17:50