2
2
3
3
import android .content .Intent ;
4
4
import android .os .Bundle ;
5
- import android .widget .Button ;
6
- import android .widget .TextView ;
7
5
8
6
import androidx .appcompat .app .AlertDialog ;
9
7
import androidx .appcompat .app .AppCompatActivity ;
10
- import androidx .appcompat .widget .Toolbar ;
11
8
import androidx .vectordrawable .graphics .drawable .VectorDrawableCompat ;
12
9
13
10
import com .facebook .drawee .drawable .ProgressBarDrawable ;
14
11
import com .facebook .drawee .generic .GenericDraweeHierarchyBuilder ;
15
- import com .facebook .drawee .view .SimpleDraweeView ;
16
12
13
+ import fr .free .nrw .commons .databinding .ActivityQuizBinding ;
17
14
import java .util .ArrayList ;
18
15
19
- import butterknife .BindView ;
20
- import butterknife .ButterKnife ;
21
- import butterknife .OnClick ;
22
16
import fr .free .nrw .commons .R ;
23
17
24
18
public class QuizActivity extends AppCompatActivity {
25
19
26
- @ BindView (R .id .question_image ) SimpleDraweeView imageView ;
27
- @ BindView (R .id .question_text ) TextView questionText ;
28
- @ BindView (R .id .question_title ) TextView questionTitle ;
29
- @ BindView (R .id .quiz_positive_answer ) Button positiveAnswer ;
30
- @ BindView (R .id .quiz_negative_answer ) Button negativeAnswer ;
31
- @ BindView (R .id .toolbar ) Toolbar toolbar ;
32
-
33
- private QuizController quizController = new QuizController ();
20
+ private ActivityQuizBinding binding ;
21
+ private final QuizController quizController = new QuizController ();
34
22
private ArrayList <QuizQuestion > quiz = new ArrayList <>();
35
23
private int questionIndex = 0 ;
36
24
private int score ;
@@ -44,15 +32,14 @@ public class QuizActivity extends AppCompatActivity {
44
32
private boolean isNegativeAnswerChecked ;
45
33
46
34
@ Override
47
- protected void onCreate (Bundle savedInstanceState ) {
35
+ protected void onCreate (final Bundle savedInstanceState ) {
48
36
super .onCreate (savedInstanceState );
49
- setContentView (R .layout .activity_quiz );
37
+ binding = ActivityQuizBinding .inflate (getLayoutInflater ());
38
+ setContentView (binding .getRoot ());
50
39
51
40
quizController .initialize (this );
52
- ButterKnife .bind (this );
53
- setSupportActionBar (toolbar );
54
- positiveAnswer = findViewById (R .id .quiz_positive_answer );
55
- negativeAnswer = findViewById (R .id .quiz_negative_answer );
41
+ setSupportActionBar (binding .toolbar .toolbar );
42
+ binding .nextButton .setOnClickListener (view -> notKnowAnswer ());
56
43
displayQuestion ();
57
44
}
58
45
@@ -65,7 +52,6 @@ public void setNextQuestion(){
65
52
}
66
53
}
67
54
68
- @ OnClick (R .id .next_button )
69
55
public void notKnowAnswer (){
70
56
customAlert ("Information" , quiz .get (questionIndex ).getAnswerMessage ());
71
57
}
@@ -75,42 +61,45 @@ public void notKnowAnswer(){
75
61
*/
76
62
@ Override
77
63
public void onBackPressed () {
78
- AlertDialog . Builder alert = new AlertDialog .Builder (this );
79
- alert .setTitle (getResources ().getString (R .string .warning ));
80
- alert .setMessage (getResources ().getString (R .string .quiz_back_button ));
81
- alert .setPositiveButton (R .string .continue_message , (dialog , which ) -> {
82
- Intent i = new Intent (QuizActivity . this , QuizResultActivity .class );
83
- dialog .dismiss ();
84
- i .putExtra ("QuizResult" ,score );
85
- startActivity (i );
86
- });
87
- alert .setNegativeButton ("Cancel" , (dialogInterface , i ) -> dialogInterface .dismiss ());
88
- AlertDialog dialog = alert .create ();
89
- dialog .show ();
64
+ new AlertDialog .Builder (this )
65
+ .setTitle (getResources ().getString (R .string .warning ))
66
+ .setMessage (getResources ().getString (R .string .quiz_back_button ))
67
+ .setPositiveButton (R .string .continue_message , (dialog , which ) -> {
68
+ final Intent intent = new Intent (this , QuizResultActivity .class );
69
+ dialog .dismiss ();
70
+ intent .putExtra ("QuizResult" , score );
71
+ startActivity (intent );
72
+ })
73
+ .setNegativeButton ("Cancel" , (dialogInterface , i ) -> dialogInterface .dismiss ())
74
+ .create ()
75
+ .show ();
90
76
}
91
77
92
78
/**
93
79
* to display the question
94
80
*/
95
81
public void displayQuestion () {
96
82
quiz = quizController .getQuiz ();
97
- questionText .setText (quiz .get (questionIndex ).getQuestion ());
98
- questionTitle .setText (getResources ().getString (R .string .question )+quiz .get (questionIndex ).getQuestionNumber ());
99
- imageView .setHierarchy (GenericDraweeHierarchyBuilder
83
+ binding .question .questionText .setText (quiz .get (questionIndex ).getQuestion ());
84
+ binding .questionTitle .setText (
85
+ getResources ().getString (R .string .question ) +
86
+ quiz .get (questionIndex ).getQuestionNumber ()
87
+ );
88
+ binding .question .questionImage .setHierarchy (GenericDraweeHierarchyBuilder
100
89
.newInstance (getResources ())
101
90
.setFailureImage (VectorDrawableCompat .create (getResources (),
102
91
R .drawable .ic_error_outline_black_24dp , getTheme ()))
103
92
.setProgressBarImage (new ProgressBarDrawable ())
104
93
.build ());
105
94
106
- imageView .setImageURI (quiz .get (questionIndex ).getUrl ());
95
+ binding . question . questionImage .setImageURI (quiz .get (questionIndex ).getUrl ());
107
96
isPositiveAnswerChecked = false ;
108
97
isNegativeAnswerChecked = false ;
109
- positiveAnswer .setOnClickListener (view -> {
98
+ binding . answer . quizPositiveAnswer .setOnClickListener (view -> {
110
99
isPositiveAnswerChecked = true ;
111
100
setNextQuestion ();
112
101
});
113
- negativeAnswer .setOnClickListener (view -> {
102
+ binding . answer . quizNegativeAnswer .setOnClickListener (view -> {
114
103
isNegativeAnswerChecked = true ;
115
104
setNextQuestion ();
116
105
});
@@ -122,34 +111,36 @@ R.drawable.ic_error_outline_black_24dp, getTheme()))
122
111
public void evaluateScore () {
123
112
if ((quiz .get (questionIndex ).isAnswer () && isPositiveAnswerChecked ) ||
124
113
(!quiz .get (questionIndex ).isAnswer () && isNegativeAnswerChecked ) ){
125
- customAlert (getResources ().getString (R .string .correct ),quiz .get (questionIndex ).getAnswerMessage () );
114
+ customAlert (getResources ().getString (R .string .correct ),
115
+ quiz .get (questionIndex ).getAnswerMessage ());
126
116
score ++;
127
117
} else {
128
- customAlert (getResources ().getString (R .string .wrong ), quiz .get (questionIndex ).getAnswerMessage ());
118
+ customAlert (getResources ().getString (R .string .wrong ),
119
+ quiz .get (questionIndex ).getAnswerMessage ());
129
120
}
130
121
}
131
122
132
123
/**
133
124
* to display explanation after each answer, update questionIndex and move to next question
134
- * @param title
135
- * @param Message
125
+ * @param title the alert title
126
+ * @param Message the alert message
136
127
*/
137
- public void customAlert (String title , String Message ) {
138
- AlertDialog . Builder alert = new AlertDialog .Builder (this );
139
- alert .setTitle (title );
140
- alert .setMessage (Message );
141
- alert .setPositiveButton (R .string .continue_message , (dialog , which ) -> {
142
- questionIndex ++;
143
- if (questionIndex == quiz .size ()) {
144
- Intent i = new Intent (QuizActivity . this , QuizResultActivity .class );
145
- dialog .dismiss ();
146
- i .putExtra ("QuizResult" ,score );
147
- startActivity (i );
148
- } else {
149
- displayQuestion ();
150
- }
151
- });
152
- AlertDialog dialog = alert .create ();
153
- dialog .show ();
128
+ public void customAlert (final String title , final String Message ) {
129
+ new AlertDialog .Builder (this )
130
+ .setTitle (title )
131
+ .setMessage (Message )
132
+ .setPositiveButton (R .string .continue_message , (dialog , which ) -> {
133
+ questionIndex ++;
134
+ if (questionIndex == quiz .size ()) {
135
+ final Intent intent = new Intent (this , QuizResultActivity .class );
136
+ dialog .dismiss ();
137
+ intent .putExtra ("QuizResult" , score );
138
+ startActivity (intent );
139
+ } else {
140
+ displayQuestion ();
141
+ }
142
+ })
143
+ .create ()
144
+ .show ();
154
145
}
155
146
}
0 commit comments