Professional Documents
Culture Documents
quizzler_complete;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
// Create question bank using the TrueFalse class for each item in the array
@NonNull
private TrueFalse[] mQuestionBank = new TrueFalse[] {
new TrueFalse(R.string.question_1, true),
new TrueFalse(R.string.question_2, true),
new TrueFalse(R.string.question_3, true),
new TrueFalse(R.string.question_4, true),
new TrueFalse(R.string.question_5, true),
new TrueFalse(R.string.question_6, false),
new TrueFalse(R.string.question_7, true),
new TrueFalse(R.string.question_8, false),
new TrueFalse(R.string.question_9, true),
new TrueFalse(R.string.question_10, true),
new TrueFalse(R.string.question_11, false),
new TrueFalse(R.string.question_12, false),
new TrueFalse(R.string.question_13,true)
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// In API 26 'casting' is no longer needed.
// For API 25 and lower: mTrueButton = (Button)
findViewById(R.id.true_button);
// For API 26 and higher can use: mTrueButton =
findViewById(R.id.true_button);
mTrueButton = findViewById(R.id.true_button);
mFalseButton = findViewById(R.id.false_button);
mQuestionTextView = findViewById(R.id.question_text_view);
mScoreTextView = findViewById(R.id.score);
mProgressBar = findViewById(R.id.progress_bar);
mQuestion = mQuestionBank[mIndex].getQuestionID();
mQuestionTextView.setText(mQuestion);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(true);
updateQuestion();
}
});
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(false);
updateQuestion();
}
});
mQuestion = mQuestionBank[mIndex].getQuestionID();
mQuestionTextView.setText(mQuestion);
mProgressBar.incrementProgressBy(PROGRESS_BAR_INCREMENT);
mScoreTextView.setText("Score " + mScore + "/" + mQuestionBank.length);
}
// Can cancel the Toast message if there is one on screen and a new answer
// has been submitted.
if (mToastMessage != null) {
mToastMessage.cancel();
}
if(userSelection == correctAnswer) {
mToastMessage = makeText(getApplicationContext(),
R.string.correct_toast, Toast.LENGTH_SHORT);
mScore = mScore + 1;
} else {
mToastMessage = Toast.makeText(getApplicationContext(),
R.string.incorrect_toast, Toast.LENGTH_LONG);
}
mToastMessage.show();
// This callback is received when the screen is rotated so we can save the
'state'
// of the app in a 'bundle'.
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putInt("ScoreKey", mScore);
outState.putInt("IndexKey", mIndex);
}