0

I try to create a simple quiz app that user will choose what they think the right answers are and when they ll click the submit button they can see their score each time. The quiz questions are in CheckBoxes and radioButtons. The problem is that I don't know how to display the score via Button on screen each time user clicks the button. Thanks for any help!(trying this thing 3 days!)

MainActivity.java

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

public class MainActivity extends AppCompatActivity {

    String Name;
    int score = 0;
    String rightAnswer = "";

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

    public void onCheckboxClicked(View view) {

        //This method is called when the checkBox is clicked

        //first right box
        CheckBox Aristotle = (CheckBox) findViewById(R.id.firstLeftCheckBox);
        boolean hasAristotle = Aristotle.isChecked();

        //first left box
        CheckBox Pythagoras = (CheckBox) findViewById(R.id.firstRightCheckBox);
        boolean hasPythagoras = Pythagoras.isChecked();

        //second left box
        CheckBox William_McKinley = (CheckBox) findViewById(R.id.secondLeftCheckBox);
        boolean hasWilliamMcKinley = William_McKinley.isChecked();

        //second right box
        CheckBox Abraham_Lincoln = (CheckBox) findViewById(R.id.secondRightCheckBox);
        boolean hasAbraham_Lincoln = Abraham_Lincoln.isChecked();

        //third left box
        CheckBox China = (CheckBox) findViewById(R.id.thirdLeftCheckBox);
        boolean hasChina = China.isChecked();

        //third right box
        CheckBox Thailand = (CheckBox) findViewById(R.id.thirdRightCheckBox);
        boolean hasThailand = Thailand.isChecked();

        //fourth left box
        CheckBox Louis_XIV = (CheckBox) findViewById(R.id.fourthLeftCheckBox);
        boolean hasLouis_XIV = Louis_XIV.isChecked();

        //fourth right box
        CheckBox Michael_I = (CheckBox) findViewById(R.id.fourthRightCheckBox);
        boolean hasMichael_I = Michael_I.isChecked();

        //fifth left box
        CheckBox threeHundredThirty = (CheckBox) findViewById(R.id.fifthLeftCheckBox);
        boolean hasthreeHundredThirty = threeHundredThirty.isChecked();

        //fifth right box
        CheckBox threeHundredThirtySix = (CheckBox) findViewById(R.id.fifthRightCheckBox);
        boolean hasthreeHundredThirtySix = threeHundredThirtySix.isChecked();

        //sixth left box
        CheckBox universityOfZurich = (CheckBox) findViewById(R.id.sixthLeftCheckBox);
        boolean hasuniversityOfZurich = universityOfZurich.isChecked();

        //sixth right box
        CheckBox universityOfGermany = (CheckBox) findViewById(R.id.sixthRightCheckBox);
        boolean hasuniversityOfGermany = universityOfGermany.isChecked();

        boolean checked = ((CheckBox) view).isChecked();
        switch (view.getId()) {
            //display a toast message all right answers
            case R.id.firstLeftCheckBox:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                break;
            case R.id.secondRightCheckBox:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                break;
            case R.id.thirdLeftCheckBox:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                break;
            case R.id.fourthLeftCheckBox:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                break;
            case R.id.fifthRightCheckBox:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                break;
            case R.id.sixthLeftCheckBox:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                break;
            //display a toast message for all wrong answers
            case R.id.firstRightCheckBox:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.secondLeftCheckBox:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.thirdRightCheckBox:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.fourthRightCheckBox:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.fifthLeftCheckBox:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.sixthRightCheckBox:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    //Radio Box
    public void onRadioButtonClicked(View view) {
        RadioButton astronomyRadioButton = (RadioButton) findViewById(R.id.astronomyRadioButton);
        RadioButton philosophyRadioButton = (RadioButton) findViewById(R.id.philosophyRadioButton);

        boolean checked = ((RadioButton)view).isChecked();
        switch (view.getId()){
            case R.id.astronomyRadioButton:
                Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                break;
            case R.id.philosophyRadioButton:
                Toast.makeText(this,  "Sorry, try again!", Toast.LENGTH_SHORT).show();
                break;
 }}}
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.allyouask.hungryforhistory.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:id="@+id/nameField"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-light"
            android:inputType="text"
            android:hint="Name"
            android:textColor="#EF6C00"
            android:textSize="15sp"/>

        <TextView
            android:id="@+id/welcomeMessage"
            style="@style/WelcomeScreenText"
            android:fontFamily="sans-serif-light"
            android:text="Welcome to Hungry For History!\n         Let's get started!"/>

        <TextView
            android:id="@+id/firstQuestion"
            style="@style/QuestionsStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/welcomeMessage"
            android:paddingLeft="5dp"
            android:text="Who was born in Ancient City Stagira, Greece?"/>

        <CheckBox
            android:id="@+id/firstLeftCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/firstQuestion"
            android:text="Aristotle"
            android:onClick="onCheckboxClicked"/>

        <CheckBox
            android:id="@+id/firstRightCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/firstQuestion"
            android:layout_toRightOf="@+id/firstLeftCheckBox"
            android:text="Pythagoras"
            android:onClick="onCheckboxClicked"/>
        <TextView
            android:id="@+id/secondQuestion"
            style="@style/QuestionsStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/firstLeftCheckBox"
            android:paddingLeft="5dp"
            android:text="Who said in his last speech:...?"/>

        <CheckBox
            android:id="@+id/secondLeftCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/secondQuestion"
            android:text="William McKinley"
            android:onClick="onCheckboxClicked"/>

        <CheckBox
            android:id="@+id/secondRightCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/secondQuestion"
            android:layout_toRightOf="@+id/secondLeftCheckBox"
            android:text="Abraham Lincoln"
            android:onClick="onCheckboxClicked"/>


        <TextView
            android:id="@+id/thirdQuestion"
            style="@style/QuestionsStyle"
            android:layout_below="@+id/secondLeftCheckBox"
            android:paddingLeft="5dp"
            android:text="Where the An Lushan Rebellion took place?"/>

        <CheckBox
            android:id="@+id/thirdLeftCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/thirdQuestion"
            android:text="China"
            android:onClick="onCheckboxClicked"/>

        <CheckBox
            android:id="@+id/thirdRightCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/thirdQuestion"
            android:layout_toRightOf="@+id/thirdLeftCheckBox"
            android:text="Thailand"
            android:onClick="onCheckboxClicked"/>

        <TextView
            android:id="@+id/fourthQuestion"
            style="@style/QuestionsStyle"
            android:layout_below="@+id/thirdLeftCheckBox"
            android:text="Who was the most famous exemplar of absolute monarchy in France?"/>

        <CheckBox
            android:id="@+id/fourthLeftCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/fourthQuestion"
            android:text="Louis XIV"
            android:onClick="onCheckboxClicked"/>

        <CheckBox
            android:id="@+id/fourthRightCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/fourthQuestion"
            android:layout_toRightOf="@+id/fourthLeftCheckBox"
            android:text="Michael I"
            android:onClick="onCheckboxClicked"/>

        <TextView
            android:id="@+id/fifthQuestion"
            style="@style/QuestionsStyle"
            android:layout_below="@+id/fourthLeftCheckBox"
            android:text="When Alexander The Great lived?"/>

        <CheckBox
            android:id="@+id/fifthLeftCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/fifthQuestion"
            android:text="330-323 BC"
            android:onClick="onCheckboxClicked"/>

        <CheckBox
            android:id="@+id/fifthRightCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/fifthQuestion"
            android:layout_toRightOf="@+id/fifthLeftCheckBox"
            android:text="336-323 BC"
            android:onClick="onCheckboxClicked"/>

        <TextView
            android:id="@+id/sixthQuestion"
            style="@style/QuestionsStyle"
            android:layout_below="@+id/fifthLeftCheckBox"
            android:text="Where Albert Einstein studied?"/>

        <CheckBox
            android:id="@+id/sixthLeftCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/sixthQuestion"
            android:text="University of Zurich"
            android:onClick="onCheckboxClicked"/>

        <CheckBox
            android:id="@+id/sixthRightCheckBox"
            style="@style/CheckBoxStyle"
            android:layout_below="@+id/sixthQuestion"
            android:layout_toRightOf="@+id/sixthLeftCheckBox"
            android:text="University of Germany"
            android:onClick="onCheckboxClicked"/>

        <TextView
            android:id="@+id/seventhQuestion"
            style="@style/QuestionsStyle"
            android:layout_below="@+id/sixthLeftCheckBox"
            android:text="What was the main interest of Democritus?"/>

        <RadioGroup
            android:id="@+id/radioButtons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/seventhQuestion"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/astronomyRadioButton"
                style="@style/CheckBoxStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Mathematics-Astronomy"
                android:onClick="onRadioButtonClicked"/>

            <RadioButton
                android:id="@+id/philosophyRadioButton"
                style="@style/CheckBoxStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Philosophy-Psychology"
                android:onClick="onRadioButtonClicked"/>
        </RadioGroup>

        <Button
            android:id="@+id/submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/radioButtons"
            android:layout_centerHorizontal="true"
            android:layout_margin="10dp"
            android:background="@color/backgroundColor"
            android:text="Submit"
            android:textColor="@color/textColor"
            android:onClick="submitButton"/>


    </RelativeLayout>

</ScrollView>`enter code here`
Eleni_M
  • 3
  • 7

3 Answers3

0
Button btnSubmit;

in onCreate():

btnSubmit = (Button) findViewById(R.id.submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                btn.setText((String) score)
            }
        });

in your switch() when answer is right add:

score += X
Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
  • Hi Luca, Thanks a lot for your answer. I am trying do what you suggest but app crash. it's my bad but can't find what. here is my code again after change(i change some just to try it) – Eleni_M Jul 11 '16 at 08:24
  • update with your code & error log, i'll help find out what's the problem – Luca Nicoletti Jul 11 '16 at 08:25
  • No, no no, please, update your question with the formatted code. I can't read code like that. – Luca Nicoletti Jul 11 '16 at 08:30
  • sory!!Thank you Log: – Eleni_M Jul 11 '16 at 09:19
  • 07-11 09:18:19.437 1564-2259/com.android.inputmethod.latin E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb3fd6d00 07-11 09:18:19.974 2808-2844/com.allyouask.hungryforhistory E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb3fd3570 07-11 09:18:22.405 2808-2808/com.allyouask.hungryforhistory W/ResourceType: No package identifier when getting value for resource number 0x00000001 07-11 09:18:22.405 2808-2808/com.allyouask.hungryforhistory D/AndroidRuntime: Shutting down VM 07-11 09:18:22.405 2808-2808/com.allyouask.hungryforhistory E/AndroidRuntime: FATAL EXCEPTION: main – Eleni_M Jul 11 '16 at 09:21
  • 07-11 09:21:36.390 6123-6123/com.allyouask.hungryforhistory E/AndroidRuntime: FATAL EXCEPTION: main Process: com.allyouask.hungryforhistory, PID: 6123 android.content.res.Resources$NotFoundException: String resource ID #0x1 at android.content.res.Resources.getText(Resources.java:312) at android.widget.TextView.setText(TextView.java:4417) – Eleni_M Jul 11 '16 at 09:22
  • at com.allyouask.hungryforhistory.MainActivity$1.onClick(MainActivity.java:25) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) – Eleni_M Jul 11 '16 at 09:24
0

try this

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.rb1:
                score+=5;
                break; 

                case R.id.rb5:
                    score+=5;
                break;

                //…………
            }


        }
    });
Cgx
  • 753
  • 3
  • 6
  • Should I write the setOnCheckedChangeListener in onCreate method and transfer the switch there? Sorry, I am newbie! – Eleni_M Jul 11 '16 at 08:29
0

Increment score variable when answer is right. So that you have the result inside your score variable.

You have to define onClickListener for your submit button as follows: Define the following method inside your MainActivity Class. Name of the method should be same as you had defined in your layout file:

android:onClick="submitButton"

Inside your MainActivity:

  public void submitButton(View v) {
   //display score either using Toast or create a TextView in layout and display inside it.

  }

For more info for onClickListeners: API Guides

You can also create a new Activity for displaying the score. For starting a new Activity and passing data to it refer :How to send string from one activity to another?

Community
  • 1
  • 1
OhmnioX
  • 473
  • 3
  • 13