I used this Youtube Tutorial: https://www.youtube.com/watch?v=AkUfACbi6BE to create an Open Answer quiz in Android Studio using Java. I followed the tutorial exactly, but when I ran the code on a device, even if I answer questions incorrectly, it says I won the game. I am not sure why it isn’t validating answers correctly. Any tips on how to fix this would be great! The code for the app is below, since the video does not provide source code, thought it would be useful.
Database Class:
package com.tests.shortanswerquiz; public class Database { public static String[] questions = { "Which is the first planet in the Solar system?", "Which is the second planet in the Solar system?", "Which is the third planet in the Solar system?", "Which is the fourth planet in the Solar system?", "Which is the fifth planet in the Solar system?", "Which is the sixth planet in the Solar system?", "Which is the seventh planet in the Solar system?", "Which is the eighth planet in the Solar system?", "Which is a dwarf planet in the Solar system?", }; public static String[] answers = { "mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune", "pluto", }; }
Item Class:
package com.tests.shortanswerquiz; public class Item { private String question, answer; public Item(String question, String answer) { this.question = question; this.answer = answer; } public String getQuestion() { return question; } public String getAnswer() { return answer; } }
MainActivity Class:
package com.tests.shortanswerquiz; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Random; public class MainActivity extends AppCompatActivity { Button b_continue; TextView tv_question; EditText et_answer; List<Item> questions; int curQuestion = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b_continue = (Button) findViewById(R.id.b_continue); tv_question = (TextView) findViewById(R.id.tv_question); et_answer = (EditText) findViewById(R.id.et_answer); b_continue.setVisibility(View.INVISIBLE); questions = new ArrayList<>(); for (int i = 0; i < Database.questions.length; i++) { questions.add(new Item(Database.questions[i], Database.answers[i])); } Collections.shuffle(questions); tv_question.setText(questions.get(curQuestion).getQuestion()); et_answer.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if(et_answer.getText().toString().equalsIgnoreCase(questions.get(curQuestion).getAnswer())) { b_continue.setVisibility(View.VISIBLE); } else { b_continue.setVisibility(View.VISIBLE); } } @Override public void afterTextChanged(Editable s) { } }); b_continue.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(curQuestion < (Database.questions.length - 1)) { curQuestion++; tv_question.setText(questions.get(curQuestion).getQuestion()); b_continue.setVisibility(View.INVISIBLE); et_answer.setText(""); } else { Toast.makeText(MainActivity.this, "You won the game!", Toast.LENGTH_SHORT).show(); finish(); } } }); } }
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=".MainActivity"> <Button android:id="@+id/b_continue" android:layout_width="match_parent" android:layout_height="61dp" android:text="CONTINUE" /> <TextView android:id="@+id/tv_question" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/b_continue" android:layout_centerHorizontal="true" android:gravity="center" android:padding="10dp" android:text=" " /> <EditText android:id="@+id/et_answer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tv_question" android:layout_centerHorizontal="true" android:ems="10" android:hint="answer" android:inputType="textPersonName" android:text="Name" /> </RelativeLayout>
Advertisement
Answer
The reason your code is failing is due to this piece of code.
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { if(et_answer.getText().toString().equalsIgnoreCase(questions.get(curQuestion).getAnswer())) { b_continue.setVisibility(View.VISIBLE); } else { b_continue.setVisibility(View.VISIBLE);//set this to View.INVISIBLE instead } }
The way the code is written is such that you cannot move to the next part of the code until you give the correct answer . When you set it to View.VISIBLE it lets you go to the next question without answering it correctly.