0

I have a simple quiz app where users can see their score from the scoreButton and reset their score from the resetButton. Questions are in RadioBoxes.The problem is that when the user clicks on resetButton, button shows 0 BUT doesn't clean the radioBoxes user had choose to start from the beginning. Something I miss in my code but can't figure it out. Any help appreciated a lot!

MainActivity.java

public class MainActivity extends AppCompatActivity {

    String Name;
    int score = 0;
    Button submitButton;
    Button resetButton;

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

        //user input name
         final EditText nameField = (EditText)findViewById(R.id.nameField);
          Name = nameField.getText().toString();

        //submitButton shows user score
        submitButton = (Button) findViewById(R.id.submitButton);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitButton.setText("Your score is:" + score);
            }
        });
        //resetButton reset score to 0
        resetButton = (Button) findViewById(R.id.resetButton);
        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitButton.setText((String.valueOf(0)));
            }
        });
    }
    //This method is called when Radio Buttons are clicked
    public void onRadioButtonClicked(View view) {

        boolean checked = ((RadioButton)view).isChecked();
        switch (view.getId()) {
            //display a toast message all right answers
            case R.id.firstLeftRadioButton:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                score ++;
                break;
            case R.id.secondRightRadioButton:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                score++;
                break;
            case R.id.thirdLeftRadioButton:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                score++;
                break;
            case R.id.fourthLeftRadioButton:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                score++;
                break;
            case R.id.fifthRightRadioButton:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                score++;
                break;
            case R.id.sixthLeftRadioButton:
                if (checked)
                    Toast.makeText(this, "Hooray!Your answer is right", Toast.LENGTH_SHORT).show();
                score++;
                break;

            //display a toast message for all wrong answers
            case R.id.firstRightRadioButton:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                score--;
                break;
            case R.id.secondLeftRadioButton:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                score--;
                break;
            case R.id.thirdRightRadioButton:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                score--;
                break;
            case R.id.fourthRightCheckBox:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                score--;
                break;
            case R.id.fifthLeftRadioButton:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                score--;
                break;
            case R.id.sixthRightRadioButton:
                if (checked)
                    Toast.makeText(this, "Sorry, try again!", Toast.LENGTH_SHORT).show();
                score--;
                break;
        }
    }

activity_main.xml

<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?"/>

    <TextView
        android:id="@+id/secondQuestion"
        style="@style/QuestionsStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/firstLeftRadioButton"
        android:paddingLeft="5dp"
        android:text="Who said in his last speech:With malice toward none;...let us strive on to finish the work we are in;to bind up the nation's wounds;into care for him who shall have borne the battle and for his widow and his orphans?"/>

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

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

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

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

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

    <RadioGroup
        android:id="@+id/firstGroupRadioButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/firstQuestion"
        android:orientation="horizontal"/>

    <RadioButton
        android:id="@+id/firstLeftRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/firstQuestion"
        android:text="Aristotle"
        android:onClick="onRadioButtonClicked"/>

    <RadioButton
        android:id="@+id/firstRightRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/firstQuestion"
        android:layout_toRightOf="@+id/firstLeftRadioButton"
        android:text="Pythagoras"
        android:onClick="onRadioButtonClicked"/>

    <RadioGroup
        android:id="@+id/secondGroupRadioButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/secondQuestion"
        android:orientation="horizontal"/>
    <RadioButton
        android:id="@+id/secondLeftRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/secondQuestion"
        android:text="William McKinley"
        android:onClick="onRadioButtonClicked"/>

    <RadioButton
        android:id="@+id/secondRightRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/secondQuestion"
        android:layout_toRightOf="@+id/secondLeftRadioButton"
        android:text="Abraham Lincoln"
        android:onClick="onRadioButtonClicked"/>

    <RadioGroup
        android:id="@+id/thirdGroupRadioButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/thirdQuestion"
        android:orientation="horizontal"/>

    <RadioButton
        android:id="@+id/thirdLeftRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/thirdQuestion"
        android:text="China"
        android:onClick="onRadioButtonClicked"/>

    <RadioButton
        android:id="@+id/thirdRightRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/thirdQuestion"
        android:layout_toRightOf="@+id/thirdLeftRadioButton"
        android:text="Thailand"
        android:onClick="onRadioButtonClicked"/>

    <RadioGroup
        android:id="@+id/fourthGroupRadioButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/fourthQuestion"
        android:orientation="horizontal"/>

    <RadioButton
        android:id="@+id/fourthLeftRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/fourthQuestion"
        android:text="Louis XIV"
        android:onClick="onRadioButtonClicked"/>

    <RadioButton
        android:id="@+id/fourthRightCheckBox"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/fourthQuestion"
        android:layout_toRightOf="@+id/fourthLeftRadioButton"
        android:text="Michael I"
        android:onClick="onRadioButtonClicked"/>

    <RadioGroup
        android:id="@+id/fifthGroupRadioButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/fifthQuestion"
        android:orientation="horizontal"/>

    <RadioButton
        android:id="@+id/fifthLeftRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/fifthQuestion"
        android:text="330-323 BC"
        android:onClick="onRadioButtonClicked"/>

    <RadioButton
        android:id="@+id/fifthRightRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/fifthQuestion"
        android:layout_toRightOf="@+id/fifthLeftRadioButton"
        android:text="336-323 BC"
        android:onClick="onRadioButtonClicked"/>

    <RadioGroup
        android:id="@+id/sixthGroupRadioButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/sixthQuestion"
        android:orientation="horizontal"/>

    <RadioButton
        android:id="@+id/sixthLeftRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/sixthQuestion"
        android:text="University of Zurich"
        android:onClick="onRadioButtonClicked"/>

    <RadioButton
        android:id="@+id/sixthRightRadioButton"
        style="@style/RadioButtonStyle"
        android:layout_below="@+id/sixthQuestion"
        android:layout_toRightOf="@+id/sixthLeftRadioButton"
        android:text="University of Germany"
        android:onClick="onRadioButtonClicked"/>

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

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

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

    <TextView
        android:id="@+id/score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>


    <Button
        android:id="@+id/submitButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/seventhGroupRadioButtons"
        android:layout_marginBottom="3dp"
        android:background="@color/backgroundColor"
        android:text="Submit"
        android:textColor="@color/textColor"
        android:onClick="OnClick"/>

    <Button
        android:id="@+id/resetButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/submitButton"
        android:layout_marginBottom="3dp"
        android:onClick="OnClick"
        android:background="@color/backgroundResetColor"
        android:textColor="@color/textColor"
        android:text="Reset"
        android:textAllCaps="true"/>



</RelativeLayout>

mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
Eleni_M
  • 3
  • 7

2 Answers2

0

put

RadioGroup.clearCheck();

to your resetButton onClick.

This might help you.

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
0

Do something like this

   RadioGroup radioGroup;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     radioGroup= (RadioGroup)findViewById(R.id.firstGroupRadioButtons);
    //resetButton reset score to 0
    resetButton = (Button) findViewById(R.id.resetButton);
    resetButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            submitButton.setText((String.valueOf(0)));
            radioGroup.clearCheck();

        }
    });
  }

You need to declare your radio group then define it! So you can clear all checks on radio button.

Hope this helps!

Android Nerd
  • 625
  • 1
  • 8
  • 13
  • Hi thanks for your answer. I did this but radioButtons still not clean.The result stays as user has choose. – Eleni_M Jul 12 '16 at 13:07
  • radioGroup = (RadioGroup)findViewById(R.id.firstGroupRadioButtons); //resetButton reset score to 0 resetButton = (Button) findViewById(R.id.resetButton); resetButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { submitButton.setText((String.valueOf(0))); radioGroup.clearCheck(); – Eleni_M Jul 12 '16 at 13:15
  • How could each question unchecked and quiz be clean again for user to start from the beginning? – Eleni_M Jul 12 '16 at 13:17