0

I wrote the XML for a Quiz App.

There will be (10) questions, some RadioGroup and some CheckBox, but the type of questions will not be in any order. So the 1st is radiogroup, the 2nd checkbox, the 3rd, 4th, and 5th radiogroup, the 6th and 7th checkbox, etc.

The RadioGroup obviously has only one correct answer, and the CheckBox have two or three correct answers.

I want to create a "CHECK QUIZ" button, which displays a message at the top of the scrolling quiz giving the number correct, and also displaying a large red "X" in red at the front of each wrong answer.

I am not sure where to start for the Java code in my MainActivity java file.


MainActivity.java


package com.example.android.quiztest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.EditText;
import android.widget.TextView;

/**
 * package com.example.android.quiztest;
 * This app displays a radio button and checkbox quiz, and then grades the quiz, *  displaying the score and identifying the incorrect answers.
 **/

public class MainActivity extends AppCompatActivity {

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

activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.quiztest.MainActivity"
    android:fillViewport="false">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/dimension_1"
        android:orientation="vertical">


        <EditText
            android:id="@+id/name_text_view_STUDENT"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/text_person_name"
            android:inputType="textPersonName" />

        <TextView
            android:id="@+id/label_text_view_DIRECTIONS"
            style="@style/HeaderTextStyle"
            android:textSize="20sp"
            android:layout_marginTop="@dimen/dimension_1"
            android:text="@string/text_directions" />


        <TextView
            android:id="@+id/label_text_view_1"
            style="@style/HeaderTextStyle"
            android:layout_marginTop="@dimen/dimension_1"
            android:text="@string/text_1" />

        <RadioGroup
            android:id="@+id/radioGroup_1"
            style="@style/HeaderTextStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">


            <RadioButton
                android:id="@+id/radioButton_1a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimension_1"
                android:paddingLeft="@dimen/dimension_4"
                android:paddingRight="@dimen/dimension_5"
                android:text="@string/text_1a"
                android:textSize="@dimen/text_size_1" />

            <RadioButton
                android:id="@+id/radioButton_1b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimension_1"
                android:paddingLeft="@dimen/dimension_4"
                android:paddingRight="@dimen/dimension_5"
                android:text="@string/text_1b"
                android:textSize="@dimen/text_size_1" />

            <RadioButton
                android:id="@+id/radioButton_1c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimension_1"
                android:paddingLeft="@dimen/dimension_4"
                android:paddingRight="@dimen/dimension_5"
                android:text="@string/text_1c"
                android:textSize="@dimen/text_size_1" />

        </RadioGroup>

        <View
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:layout_marginTop="25dp"
            android:background="#c0c0c0" />

        <TextView
            android:id="@+id/label_text_view_2"
            style="@style/HeaderTextStyle"
            android:layout_marginTop="@dimen/dimension_1"
            android:text="@string/text_2" />



            <CheckBox
                android:id="@+id/checkBox_2a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/dimension_4"
                android:paddingRight="@dimen/dimension_5"
                android:text="@string/text_2a"
                android:textSize="@dimen/text_size_1" />

            <CheckBox
                android:id="@+id/checkBox_2b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimension_1"
                android:paddingLeft="@dimen/dimension_4"
                android:paddingRight="@dimen/dimension_5"
                android:text="@string/text_2b"
                android:textSize="@dimen/text_size_1" />

            <CheckBox
                android:id="@+id/checkBox_2c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimension_1"
                android:paddingLeft="@dimen/dimension_4"
                android:paddingRight="@dimen/dimension_5"
                android:text="@string/text_2c"
                android:textSize="@dimen/text_size_1" />



    </LinearLayout>


</ScrollView>

strings.xml

<resources>

    <string name="text_button_1">check answers</string>

    <string name="app_name">Quiz Test</string>
    <string name="text_person_name">Type student name here.</string>
    <string name="text_directions">Select ALL correct answers:</string>

    <string name="text_1">1) probability distribution</string>
    <string name="text_1a">1a) A smooth curve indicating the frequency distribution for a discontinuous random variable.</string>
    <string name="text_1b">1b) A discontinuous dot diagram showing the frequency distribution for a random variable.</string>
    <string name="text_1c">1c) A smooth curve indicating the frequency distribution for a continuous random variable.</string>


    <string name="text_2">2)    normal distribution</string>
    <string name="text_2a">2a)   A smooth double-peak bell-shaped curve symmetrical about the mean.</string>
    <string name="text_2b">2b)   A smooth single-peak curve </string>
    <string name="text_2c">3c)   A bell-shaped curve symmetrical about the mean. </string>

</resources>
JavaMon
  • 149
  • 1
  • 4
  • 14

2 Answers2

1

I updated the XML to provide the ability to mark a red X in front of the wrong answers:

file: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="false"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/dimension_1"
        android:orientation="vertical">


        <EditText
            android:id="@+id/name_textView_STUDENT"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/text_person_name"
            android:inputType="textPersonName" />

        <TextView
            android:id="@+id/textView_directions"
            style="@style/HeaderTextStyle"
            android:layout_marginTop="@dimen/dimension_1"
            android:text="@string/text_directions"
            android:textSize="20sp" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dimension_1"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/incorrect_question_1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginTop="@dimen/dimension_1"
                android:gravity="center"
                android:text="X"
                android:textColor="#ff0000"
                android:textSize="30dp"
                android:visibility="gone" />

            <TextView
                android:id="@+id/textView_1"
                style="@style/HeaderTextStyle"
                android:layout_marginTop="@dimen/dimension_1"
                android:text="@string/text_1" />
        </LinearLayout>


        <RadioGroup
            android:id="@+id/radioGroup_1"
            style="@style/HeaderTextStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">


            <RadioButton
                android:id="@+id/radioButton_1a"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimension_1"
                android:paddingLeft="@dimen/dimension_4"
                android:paddingRight="@dimen/dimension_5"
                android:textSize="@dimen/text_size_1"
                android:text="@string/text_1a"
                android:onClick="onClick_1a"/>


            <RadioButton
                android:id="@+id/radioButton_1b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimension_1"
                android:paddingLeft="@dimen/dimension_4"
                android:paddingRight="@dimen/dimension_5"
                android:text="@string/text_1b"
                android:textSize="@dimen/text_size_1"
                android:onClick="onClick_2a"/>/>

            <RadioButton
                android:id="@+id/radioButton_1c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimension_1"
                android:paddingLeft="@dimen/dimension_4"
                android:paddingRight="@dimen/dimension_5"
                android:text="@string/text_1c"
                android:textSize="@dimen/text_size_1"
                android:onClick="onClick_3a"/>

        </RadioGroup>

And developed the Java grading logic for the radio groups and checkboxes:

file: MainActivity.java

package com.example.android.quiztest2;

/**
 * *Below added my unique package name "com.example.android.justjava4"
 */

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;


/**
 * package com.example.android.justjava4;
 * This app displays an order form to order coffee, and displays the information.
 * quantity is a global variable
 **/
public class MainActivity extends AppCompatActivity {
    private RadioButton radioButton_1a, radioButton_1b, radioButton_1c, radioButton_4a, radioButton_4b, radioButton_4c,
            radioButton_5a, radioButton_5b, radioButton_5c, radioButton_6a, RadioButton_6b, radioButton_6c,
            radioButton_7a, radioButton_7b, radioButton_7c, radioButton_9a, radioButton_9b, radioButton_9c;

    private CheckBox checkBox_2a, checkBox_2b, checkBox_2c, checkBox_3a, checkBox_3b, checkBox_3c, checkBox_8a, checkBox_8b, checkBox_8c;

    int grade = 0;

    public void Score(int grade) {
        if (radioButton_1c.isChecked())  grade++;

        if (radioButton_4c.isChecked())  grade++;

        if (radioButton_5c.isChecked()) grade++;

        if (radioButton_6c.isChecked()) grade++;

        if (radioButton_7c.isChecked()) grade++;

        if (radioButton_9c.isChecked()) grade++;

        if (checkBox_2a.isChecked() && !checkBox_2b.isChecked() && checkBox_2c.isChecked()) grade++;

        if (checkBox_3a.isChecked() && checkBox_3b.isChecked() && checkBox_3c.isChecked()) grade++;

        if (checkBox_8a.isChecked() && !checkBox_8b.isChecked() && checkBox_8c.isChecked()) grade++;

    }


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


        final Context currentContext = this;
        Button button_grade_quiz = (Button)
                findViewById(R.id.button_grade_quiz);
        button_grade_quiz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                boolean answerCorrect1 = false;

                TextView incorrectQuestionOne = (TextView) findViewById(R.id.incorrect_question_1);

                if (!answerCorrect1) {

                    incorrectQuestionOne.setVisibility(View.VISIBLE);
                } else {

                    incorrectQuestionOne.setVisibility(View.GONE);

                }

            }
        });
    }
}

Not sure how to connect the grading function to the other java methods.

JavaMon
  • 149
  • 1
  • 4
  • 14
0

Simple Answer: Add a button to your xml, and assign it an id. For example, the xml would most likely look something like this:

    <Button
        android:id="@+id/quiz_complete_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Finish Quiz"
        android:textSize="@dimen/text_size_1"
        android:layout_gravity="center"/>

Then, in your MainActivity AFTER setContentView is called, you will find your button view by its id, and set an onClickListener to it. My sample code below includes a rough test that will show a Toast message when the button is clicked so that you can see the button is hooked up correctly to the listener.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Context currentContext = this;
    Button quizAnsweredButton = (Button) findViewById(R.id.quiz_complete_button);
    quizAnsweredButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do something
            Toast.makeText(currentContext, "TESTING", Toast.LENGTH_LONG).show();
        }
    });
}

(Note - to clarify, the bold above is to indicate that you can only reference views once they are shown - which is what is done by setContentView above. If you try to call findViewById before setContentView, you will receive a crash.)

Here is where it gets more complicated. Without writing the whole of the solution, what I would do in order to reach your goal is to add the entirety of what you would like the quiz to look like, if the user submits the quiz with all answers wrong, in xml. I have created the xml for question 1. (I've added a horizontal LinearLayout in order to preserve the rest of your xml, but for the complexity of your view, I would recommend using a RelativeLayout) As you can see, the visibility of the incorrect label is set to "gone". This means the view will not be seen, but also will not take up space with the parent view. "invisible" would hide the view, but its space would still be filled.

Now, when the test is submitted, we will show the incorrect answer label. In your complete solution, of course, you will evaluate whether the answer is correct or not.

XML for question one label:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/question_1_incorrect_label"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="@color/red"
            android:gravity="center"
            android:visibility="gone"/>
        <TextView
            android:id="@+id/label_text_view_1"
            style="@style/HeaderTextStyle"
            android:layout_marginTop="@dimen/dimension_1"
            android:text="@string/text_1" />
    </LinearLayout>

Updated MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Context currentContext = this;
    Button quizAnsweredButton = (Button) findViewById(R.id.quiz_complete_button);
    quizAnsweredButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean answer1Correct = false;
            //TODO: Evaluate the actual value of answer1Correct boolean.
            TextView questionOneIncorrectLabel = (TextView)findViewById(R.id.question_1_incorrect_label);
            if (!answer1Correct) {
                questionOneIncorrectLabel.setVisibility(View.VISIBLE);
            }
            else {
                questionOneIncorrectLabel.setVisibility(View.GONE);
            }
        }
    });
}

Note - in colors.xml, added:

<color name="red">#FF0000</color

The rest should just be some legwork to get it working, as well as the code within MainActivity to analyze whether or not each answer is correct.

Finally, a hint to simplify your correct answer message. You can create a string constant with a placeholder for a value to be added later. For example:

<string name="text_quiz_complete_answers">You answered %d question(s) correctly.</string>

When pulling this string value from the strings.xml file, you reference it like this:

String.format(getString(R.string.text_quiz_complete_answers), 5)

With 5 just being a placeholder. Instead there should be the numerical value you calculated.

For more info on formatting strings like this, I would reference this post: Are parameters in strings.xml possible?

Community
  • 1
  • 1
Matt Shaw
  • 1
  • 1