Skip to content

Commit 712b6a2

Browse files
committed
initialize app
1 parent b4a07c3 commit 712b6a2

File tree

182 files changed

+5200
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

182 files changed

+5200
-4
lines changed

QuizApp/.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

QuizApp/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id 'com.android.application'
33
id 'kotlin-android'
4+
id 'kotlin-android-extensions'
45
}
56

67
android {
Lines changed: 224 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,237 @@
11
package com.example.quizapp
22

3-
import androidx.appcompat.app.AppCompatActivity
3+
import android.content.Intent
4+
import android.graphics.Color
5+
import android.graphics.Typeface
46
import android.os.Bundle
57
import android.util.Log
8+
import android.view.View
9+
import android.widget.TextView
10+
import androidx.appcompat.app.AppCompatActivity
11+
import androidx.core.content.ContextCompat
612
import com.example.quizapp.data.Constants
13+
import com.example.quizapp.data.Question
14+
import kotlinx.android.synthetic.main.*
715

816
class QuizQuestionActivity : AppCompatActivity() {
917
override fun onCreate(savedInstanceState: Bundle?) {
1018
super.onCreate(savedInstanceState)
1119
setContentView(R.layout.activity_quiz_question)
1220

13-
// get the question from constants
14-
val questionList = Constants.getQuestions()
15-
Log.i("Question size", "${questionList.size}")
21+
private var mCurrentPosition: Int = 1 // Default and the first question position
22+
private var mQuestionsList: ArrayList<Question>? = null
23+
24+
private var mSelectedOptionPosition: Int = 0
25+
private var mCorrectAnswers: Int = 0
26+
27+
// TODO (STEP 3: Create a variable for getting the name from intent.)
28+
// START
29+
private var mUserName: String? = null
30+
// END
31+
32+
/**
33+
* This function is auto created by Android when the Activity Class is created.
34+
*/
35+
override fun onCreate(savedInstanceState: Bundle?) {
36+
//This call the parent constructor
37+
super.onCreate(savedInstanceState)
38+
// This is used to align the xml view to this class
39+
setContentView(R.layout.activity_quiz_questions)
40+
41+
// TODO (STEP 4: Get the NAME from intent and assign it the variable.)
42+
// START
43+
mUserName = intent.getStringExtra(Constants.USER_NAME)
44+
// END
45+
46+
mQuestionsList = Constants.getQuestions()
47+
48+
setQuestion()
49+
50+
tv_option_one.setOnClickListener(this)
51+
tv_option_two.setOnClickListener(this)
52+
tv_option_three.setOnClickListener(this)
53+
tv_option_four.setOnClickListener(this)
54+
btn_submit.setOnClickListener(this)
55+
}
56+
57+
override fun onClick(v: View?) {
58+
59+
when (v?.id) {
60+
61+
R.id.tv_option_one -> {
62+
63+
selectedOptionView(tv_option_one, 1)
64+
}
65+
66+
R.id.tv_option_two -> {
67+
68+
selectedOptionView(tv_option_two, 2)
69+
}
70+
71+
R.id.tv_option_three -> {
72+
73+
selectedOptionView(tv_option_three, 3)
74+
}
75+
76+
R.id.tv_option_four -> {
77+
78+
selectedOptionView(tv_option_four, 4)
79+
}
80+
81+
R.id.btn_submit -> {
82+
83+
if (mSelectedOptionPosition == 0) {
84+
85+
mCurrentPosition++
86+
87+
when {
88+
89+
mCurrentPosition <= mQuestionsList!!.size -> {
90+
91+
setQuestion()
92+
}
93+
else -> {
94+
95+
// TODO (STEP 5: Now remove the toast message and launch the result screen which we have created and also pass the user name and score details to it.)
96+
// START
97+
val intent =
98+
Intent(this@QuizQuestionsActivity, ResultActivity::class.java)
99+
intent.putExtra(Constants.USER_NAME, mUserName)
100+
intent.putExtra(Constants.CORRECT_ANSWERS, mCorrectAnswers)
101+
intent.putExtra(Constants.TOTAL_QUESTIONS, mQuestionsList!!.size)
102+
startActivity(intent)
103+
finish()
104+
// END
105+
}
106+
}
107+
} else {
108+
val question = mQuestionsList?.get(mCurrentPosition - 1)
109+
110+
// This is to check if the answer is wrong
111+
if (question!!.correctAnswer != mSelectedOptionPosition) {
112+
answerView(mSelectedOptionPosition, R.drawable.wrong_option_border_bg)
113+
}
114+
else {
115+
mCorrectAnswers++
116+
}
117+
118+
// This is for correct answer
119+
answerView(question.correctAnswer, R.drawable.correct_option_border_bg)
120+
121+
if (mCurrentPosition == mQuestionsList!!.size) {
122+
btn_submit.text = "FINISH"
123+
} else {
124+
btn_submit.text = "GO TO NEXT QUESTION"
125+
}
126+
127+
mSelectedOptionPosition = 0
128+
}
129+
}
130+
}
131+
}
132+
133+
/**
134+
* A function for setting the question to UI components.
135+
*/
136+
private fun setQuestion() {
137+
138+
val question = mQuestionsList!!.get(mCurrentPosition - 1) // Getting the question from the list with the help of current position.
139+
140+
defaultOptionsView()
141+
142+
if (mCurrentPosition == mQuestionsList!!.size) {
143+
btn_submit.text = "FINISH"
144+
} else {
145+
btn_submit.text = "SUBMIT"
146+
}
147+
148+
progressBar.progress = mCurrentPosition
149+
tv_progress.text = "$mCurrentPosition" + "/" + progressBar.getMax()
150+
151+
tv_question.text = question.question
152+
iv_image.setImageResource(question.image)
153+
tv_option_one.text = question.optionOne
154+
tv_option_two.text = question.optionTwo
155+
tv_option_three.text = question.optionThree
156+
tv_option_four.text = question.optionFour
157+
}
158+
159+
/**
160+
* A function to set the view of selected option view.
161+
*/
162+
private fun selectedOptionView(tv: TextView, selectedOptionNum: Int) {
163+
164+
defaultOptionsView()
165+
166+
mSelectedOptionPosition = selectedOptionNum
167+
168+
tv.setTextColor(
169+
Color.parseColor("#363A43")
170+
)
171+
tv.setTypeface(tv.typeface, Typeface.BOLD)
172+
tv.background = ContextCompat.getDrawable(
173+
this@QuizQuestionsActivity,
174+
R.drawable.selected_option_border_bg
175+
)
176+
}
177+
178+
/**
179+
* A function to set default options view when the new question is loaded or when the answer is reselected.
180+
*/
181+
private fun defaultOptionsView() {
182+
183+
val options = ArrayList<TextView>()
184+
options.add(0, tv_option_one)
185+
options.add(1, tv_option_two)
186+
options.add(2, tv_option_three)
187+
options.add(3, tv_option_four)
188+
189+
for (option in options) {
190+
option.setTextColor(Color.parseColor("#7A8089"))
191+
option.typeface = Typeface.DEFAULT
192+
option.background = ContextCompat.getDrawable(
193+
this@QuizQuestionsActivity,
194+
R.drawable.default_option_border_bg
195+
)
196+
}
197+
}
198+
199+
/**
200+
* A function for answer view which is used to highlight the answer is wrong or right.
201+
*/
202+
private fun answerView(answer: Int, drawableView: Int) {
203+
204+
when (answer) {
205+
206+
1 -> {
207+
tv_option_one.background = ContextCompat.getDrawable(
208+
this@QuizQuestionsActivity,
209+
drawableView
210+
)
211+
}
212+
2 -> {
213+
tv_option_two.background = ContextCompat.getDrawable(
214+
this@QuizQuestionsActivity,
215+
drawableView
216+
)
217+
}
218+
3 -> {
219+
tv_option_three.background = ContextCompat.getDrawable(
220+
this@QuizQuestionsActivity,
221+
drawableView
222+
)
223+
}
224+
4 -> {
225+
tv_option_four.background = ContextCompat.getDrawable(
226+
this@QuizQuestionsActivity,
227+
drawableView
228+
)
229+
}
230+
}
231+
}
232+
233+
234+
235+
16236
}
17237
}

Shrine/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

Shrine/.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Shrine/.idea/compiler.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Shrine/.idea/gradle.xml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Shrine/.idea/jarRepositories.xml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Shrine/.idea/misc.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Shrine/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

0 commit comments

Comments
 (0)