-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
104 lines (91 loc) · 3.27 KB
/
Copy pathMainActivity.java
File metadata and controls
104 lines (91 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.example.quiz;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView totalQuestionsTextView;
TextView questionTextView;
Button ansA, ansB, ansC, ansD;
Button submitBtn;
int score=0;
int totalQuestion = QuestionAnswer.question.length;
int currentQuestionIndex = 0;
String selectedanswer = "";
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
totalQuestionsTextView= findViewById(R.id.total_question);
questionTextView= findViewById(R.id.question);
ansA = findViewById(R.id.ans_A);
ansB = findViewById(R.id.ans_B);
ansC = findViewById(R.id.ans_C);
ansD = findViewById(R.id.ans_D);
submitBtn= findViewById(R.id.Submit_btn);
ansA.setOnClickListener(this);
ansB.setOnClickListener(this);
ansC.setOnClickListener(this);
ansD.setOnClickListener(this);
submitBtn.setOnClickListener(this);
totalQuestionsTextView.setText("Total questions : "+totalQuestion);
loadNewQuestion();
}
@Override
public void onClick(View v) {
ansA.setBackgroundColor(Color.WHITE);
ansB.setBackgroundColor(Color.WHITE);
ansC.setBackgroundColor(Color.WHITE);
ansD.setBackgroundColor(Color.WHITE);
Button clickedButton = (Button) v;
if(clickedButton.getId()==R.id.Submit_btn){
currentQuestionIndex++;
loadNewQuestion();
if(selectedanswer.equals(QuestionAnswer.correctAnswers[currentQuestionIndex]))
{
score++;
}
}else{
selectedanswer = clickedButton.getText().toString();
clickedButton.setBackgroundColor(Color.MAGENTA);
}
}
void loadNewQuestion() {
if(currentQuestionIndex == totalQuestion)
{
finishQuiz();
return;
}
questionTextView.setText(QuestionAnswer.question[currentQuestionIndex]);
ansA.setText(QuestionAnswer.choices[currentQuestionIndex][0]);
ansB.setText(QuestionAnswer.choices[currentQuestionIndex][1]);
ansC.setText(QuestionAnswer.choices[currentQuestionIndex][2]);
ansD.setText(QuestionAnswer.choices[currentQuestionIndex][3]);
}
void finishQuiz()
{
String passstatus;
if(score > totalQuestion*0.60)
{
passstatus = "Passed";
}
else {
passstatus = "Failed";
}
new AlertDialog.Builder(this)
.setTitle(passstatus)
.setMessage("Score is "+ score+ "Out of "+totalQuestion)
.setPositiveButton("Restart",(dialogInterface, i) -> restartQuiz() )
.setCancelable(false)
.show();}
void restartQuiz() {
score = 0;
currentQuestionIndex=0;
loadNewQuestion();
}
}