",
23 | 2
24 | )
25 | val q3 = Questions(
26 | 3,
27 | "Which of the following is used for comments in C++ ?",
28 | "/* comment */",
29 | " // comment */",
30 | "// comment",
31 | "both // comment or /* comment */",
32 | 4
33 | )
34 | val q4 = Questions(
35 | 4,
36 | "Which of the following is a correct identifier in C++ ?",
37 | " VAR_1234",
38 | " `$`var_name",
39 | "7VARNAME",
40 | " 7var_name",
41 | 1
42 | )
43 | val q5 = Questions(
44 | 5,
45 | "Which of the following is not a type of Constructor in C++ ?",
46 | "Default constructor",
47 | "Parameterized constructor",
48 | "Copy constructor",
49 | "Friend constructor",
50 | 4
51 | )
52 | val q6 = Questions(
53 | 6,
54 | "What is the size of wchar_t in C++ ?",
55 | "Based on the number of bits in the system",
56 | "2 or 4",
57 | "4",
58 | "2",
59 | 1
60 | )
61 | val q7 = Questions(
62 | 7,
63 | "Which keyword is used to define the macros in C++ ?",
64 | "#macro",
65 | "#define",
66 | "macro",
67 | "define",
68 | 2
69 | )
70 | val q8 = Questions(
71 | 8,
72 | "What is Inheritance in C++ ?",
73 | "Deriving new classes from existing classes",
74 | "Overloading of classes",
75 | "Classes with same names",
76 | "Wrapping of data into a single class",
77 | 1
78 | )
79 | val q9 = Questions(
80 | 9,
81 | " Which of the following symbol is used to declare the preprocessor directives in C++ ?",
82 | "%",
83 | "^",
84 | "#",
85 | "*",
86 | 3
87 | )
88 | val q10 = Questions(
89 | 10,
90 | "Which of the following constructors are provided by the C++ compiler if not defined in a class ?",
91 | "Copy constructor",
92 | "Default constructor",
93 | "Assignment constructor",
94 | "All of the mentioned",
95 | 4
96 | )
97 | allQuestions.add(q1)
98 | allQuestions.add(q2)
99 | allQuestions.add(q3)
100 | allQuestions.add(q4)
101 | allQuestions.add(q5)
102 | allQuestions.add(q6)
103 | allQuestions.add(q7)
104 | allQuestions.add(q8)
105 | allQuestions.add(q9)
106 | allQuestions.add(q10)
107 |
108 | return allQuestions
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_result.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
11 |
23 |
24 |
33 |
34 |
38 |
39 |
49 |
50 |
60 |
61 |
69 |
70 |
74 |
75 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # :rocket: Quiz App
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | # :gift: Hacktoberfest 2024
15 | #### :dart: Let's celebrate Hacktoberfest by getting involved in the open source community and start your journey in open source.
16 | #### :dart: This repository is open to all the members of the GitHub community. Feel free to contribute to this repository.
17 |
18 |
19 | ## :label: What is Hacktoberfest ? :computer:
20 |
21 | ➡️ Hacktoberfest is a month-long celebration of open source software run by DigitalOcean.
22 |
23 | ➡️ During the month of October, they invite you to join open-source software enthusiasts, beginners, and the developer community by contributing to open-source projects.
24 |
25 | ➡️ So, make your contributions to make this Hacktoberfest a success, with a chance to receive cool swags and give yourself a headstart in your open source journey.
26 |
27 |
28 | ## :label: Participation Rules 📝
29 |
30 | ➡️ Pull requests can be made to any public repository on GitHub, look for the "hacktoberfest" topic to know whether the project is participating or not. The pull request must contain commits you made yourself.
31 |
32 | ➡️ If a maintainer reports your pull request as 🔴 spam , it will not be counted towards your participation in Hacktoberfest.
33 |
34 | ➡️ If a maintainer reports behavior that’s not in line with the project’s code of conduct, you will be ineligible to participate.
35 |
36 |
37 | ## :label: Rules Regarding Submission of PR :bookmark:
38 | ➡️ Participant can contribute anything he wishes for.
39 |
40 | ➡️ Make sure to pull request in the respective folder.
41 |
42 | ➡️ Your PR must be valid.
43 |
44 | ➡️ Don't just copy and paste code from other website/blogs .
45 |
46 | ➡️ Submitting someone else work will be reported as 🔴spam.
47 |
48 |
49 | ## :label: Getting Started
50 |
51 | * First, fork the repository (Click the Fork button in the top right corner of the page).
52 | * Clone the forked repository to your local machine.
53 |
54 | ```markdown
55 | https://github.com/your-username/Quiz_App.git
56 | ```
57 |
58 | * Add upstream URL
59 | ```markdown
60 | https://github.com/pratyaksh1610/Quiz_App.git
61 | ```
62 |
63 | * Create a new branch
64 |
65 | ```markdown
66 | git checkout -b branch_name
67 | ```
68 |
69 | * Make your contribution
70 | * Commit and push the changes
71 |
72 | ```markdown
73 | git add .
74 | git commit -m 'Relevant message'
75 | git push origin branch_name
76 | ```
77 |
78 | * Create a new pull request from your forked repository (Click the `New Pull Request` button located at the top of your repo)
79 | * Wait for your PR review and approval from the maintainers.
80 | * :star: this repository if you liked it.
81 |
82 |
83 | ## License
84 | ```
85 | MIT License
86 | Copyright (c) 2022 Pratyaksh Khurana
87 | Permission is hereby granted, free of charge, to any person obtaining a copy
88 | of this software and associated documentation files (the "Software"), to deal
89 | in the Software without restriction, including without limitation the rights
90 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
91 | copies of the Software, and to permit persons to whom the Software is
92 | furnished to do so, subject to the following conditions:
93 | The above copyright notice and this permission notice shall be included in all
94 | copies or substantial portions of the Software.
95 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
96 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
97 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
98 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
99 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
100 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
101 | SOFTWARE.
102 | ```
103 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
23 |
24 |
33 |
34 |
45 |
46 |
51 |
52 |
61 |
62 |
69 |
70 |
75 |
76 |
77 |
87 |
88 |
89 |
90 |
98 |
99 |
103 |
104 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_questions2.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
17 |
18 |
22 |
23 |
31 |
32 |
37 |
38 |
50 |
51 |
52 |
60 |
61 |
62 |
63 |
74 |
75 |
86 |
87 |
98 |
99 |
110 |
111 |
118 |
119 |
123 |
124 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/app/src/main/java/com/pratyakshkhurana/quizapp/QuizCategories.kt:
--------------------------------------------------------------------------------
1 | package com.pratyakshkhurana.quizapp
2 |
3 | import android.app.Dialog
4 | import android.content.DialogInterface
5 | import android.content.Intent
6 | import androidx.appcompat.app.AppCompatActivity
7 | import android.os.Bundle
8 | import android.view.View
9 | import android.widget.Toast
10 | import androidx.appcompat.app.AlertDialog
11 | import androidx.core.content.res.ResourcesCompat
12 | import androidx.recyclerview.widget.LinearLayoutManager
13 | import kotlinx.android.synthetic.main.activity_quiz_categories.*
14 |
15 | class QuizCategories : AppCompatActivity(), OnClicked {
16 | private lateinit var categoryList: ArrayList
17 | private lateinit var userName: String
18 | private lateinit var categorySelected: String
19 | private lateinit var builder : AlertDialog.Builder
20 | private lateinit var alertDialog: AlertDialog
21 |
22 | override fun onCreate(savedInstanceState: Bundle?) {
23 | super.onCreate(savedInstanceState)
24 | setContentView(R.layout.activity_quiz_categories)
25 |
26 |
27 | userName = intent.getStringExtra("user").toString()
28 | //recycler view needs layout manager
29 | recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
30 |
31 | categoryList = fetchCategories()
32 | recyclerView.adapter = CategoriesAdapter(categoryList, this)
33 | }
34 |
35 | private fun fetchCategories(): ArrayList {
36 | val data: ArrayList = ArrayList()
37 | val obj1 = ResourcesCompat.getDrawable(resources, R.drawable.bg1, null)?.let {
38 | ResourcesCompat.getDrawable(resources, R.drawable.book_stack, null)?.let { it1 ->
39 | CategoryView(
40 | R.drawable.bg, "Entertainment: Books",
41 | it, it1
42 | )
43 | }
44 | }
45 | val obj2 = ResourcesCompat.getDrawable(resources, R.drawable.bg2, null)?.let {
46 | ResourcesCompat.getDrawable(resources, R.drawable.film, null)?.let { it1 ->
47 | CategoryView(
48 | R.drawable.bg,
49 | "Entertainment: Film",
50 | it, it1
51 | )
52 | }
53 | }
54 | val obj3 = ResourcesCompat.getDrawable(resources, R.drawable.bg3, null)?.let {
55 | ResourcesCompat.getDrawable(resources, R.drawable.spotify, null)?.let { it1 ->
56 | CategoryView(
57 | R.drawable.bg,
58 | "Entertainment: Music",
59 | it, it1
60 | )
61 | }
62 | }
63 | val obj4 = ResourcesCompat.getDrawable(resources, R.drawable.bg4, null)?.let {
64 | ResourcesCompat.getDrawable(resources, R.drawable.cricket, null)?.let { it1 ->
65 | CategoryView(
66 | R.drawable.bg,
67 | "Sports",
68 | it, it1
69 | )
70 | }
71 | }
72 | val obj5 = ResourcesCompat.getDrawable(resources, R.drawable.bg1, null)?.let {
73 | ResourcesCompat.getDrawable(resources, R.drawable.art, null)?.let { it1 ->
74 | CategoryView(
75 | R.drawable.bg,
76 | "Art",
77 | it, it1
78 | )
79 | }
80 | }
81 | val obj6 = ResourcesCompat.getDrawable(resources, R.drawable.bg2, null)?.let {
82 | ResourcesCompat.getDrawable(resources, R.drawable.politics, null)?.let { it1 ->
83 | CategoryView(
84 | R.drawable.bg,
85 | "Politics",
86 | it, it1
87 | )
88 | }
89 | }
90 | val obj7 = ResourcesCompat.getDrawable(resources, R.drawable.bg3, null)?.let {
91 | ResourcesCompat.getDrawable(resources, R.drawable.mythology, null)?.let { it1 ->
92 | CategoryView(
93 | R.drawable.bg,
94 | "Mythology",
95 | it, it1
96 | )
97 | }
98 | }
99 | val obj8 = ResourcesCompat.getDrawable(resources, R.drawable.bg4, null)?.let {
100 | ResourcesCompat.getDrawable(resources, R.drawable.anime, null)?.let { it1 ->
101 | CategoryView(
102 | R.drawable.bg,
103 | "Entertainment: Japanese Anime & Manga",
104 | it, it1
105 | )
106 | }
107 | }
108 | val obj9 = ResourcesCompat.getDrawable(resources, R.drawable.bg1, null)?.let {
109 | ResourcesCompat.getDrawable(resources, R.drawable.history, null)?.let { it1 ->
110 | CategoryView(
111 | R.drawable.bg,
112 | "History",
113 | it, it1
114 | )
115 | }
116 | }
117 | val obj10 = ResourcesCompat.getDrawable(resources, R.drawable.bg2, null)?.let {
118 | ResourcesCompat.getDrawable(resources, R.drawable.science, null)?.let { it1 ->
119 | CategoryView(
120 | R.drawable.bg,
121 | "Science",
122 | it, it1
123 | )
124 | }
125 | }
126 |
127 | if (obj1 != null) {
128 | data.add(obj1)
129 | }
130 | if (obj2 != null) {
131 | data.add(obj2)
132 | }
133 | if (obj3 != null) {
134 | data.add(obj3)
135 | }
136 | if (obj4 != null) {
137 | data.add(obj4)
138 | }
139 | if (obj5 != null) {
140 | data.add(obj5)
141 | }
142 | if (obj6 != null) {
143 | data.add(obj6)
144 | }
145 | if (obj7 != null) {
146 | data.add(obj7)
147 | }
148 | if (obj8 != null) {
149 | data.add(obj8)
150 | }
151 | if (obj9 != null) {
152 | data.add(obj9)
153 | }
154 | if (obj10 != null) {
155 | data.add(obj10)
156 | }
157 | return data
158 |
159 | }
160 |
161 | //through interface in adapter class
162 | override fun categoryClicked(s: CategoryView) {
163 | showDialog(s)
164 | }
165 |
166 | //Dialog for Quiz Category selection
167 | private fun showDialog(s: CategoryView) {
168 | val dialogLayout = layoutInflater.inflate(R.layout.category_dialog, null)
169 | builder = AlertDialog.Builder(this)
170 | dialogLayout.findViewById(R.id.okButton).setOnClickListener {
171 | categorySelected = s.category
172 | alertDialog.dismiss()
173 | val intent = Intent(this, QuestionsActivity::class.java)
174 | intent.putExtra("user", userName)
175 | intent.putExtra("category", categorySelected)
176 | startActivity(intent)
177 | }
178 | dialogLayout.findViewById(R.id.cancelButton).setOnClickListener {
179 | alertDialog.dismiss()
180 | }
181 | builder.setView(dialogLayout)
182 | alertDialog = builder.create()
183 | alertDialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
184 | alertDialog.show()
185 | }
186 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/category_dialog.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
24 |
25 |
32 |
33 |
41 |
42 |
43 |
48 |
49 |
55 |
56 |
62 |
63 |
64 |
65 |
70 |
71 |
77 |
78 |
84 |
85 |
86 |
87 |
92 |
93 |
99 |
100 |
106 |
107 |
108 |
109 |
114 |
115 |
121 |
122 |
128 |
129 |
130 |
131 |
132 |
141 |
142 |
148 |
149 |
159 |
160 |
161 |
162 |
163 |
172 |
173 |
179 |
180 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
--------------------------------------------------------------------------------
/app/src/main/java/com/pratyakshkhurana/quizapp/QuestionsActivity.kt:
--------------------------------------------------------------------------------
1 | package com.pratyakshkhurana.quizapp
2 |
3 | import android.animation.ObjectAnimator
4 | import android.annotation.SuppressLint
5 | import android.content.Intent
6 | import android.graphics.Color
7 | import android.graphics.Typeface
8 | import android.media.MediaPlayer
9 | import android.os.Bundle
10 | import android.view.View
11 | import android.view.animation.DecelerateInterpolator
12 | import android.widget.*
13 | import androidx.appcompat.app.AppCompatActivity
14 | import androidx.core.content.ContextCompat
15 | import kotlinx.android.synthetic.main.activity_questions2.*
16 |
17 |
18 | class QuestionsActivity : AppCompatActivity(), View.OnClickListener {
19 |
20 | private var mSelectOptionPosition: Int = 0
21 | private var mCurrentQuestionIndex: Int = 1
22 | private lateinit var mQuestionList: ArrayList
23 | private lateinit var mUsername: String
24 | private lateinit var category: String
25 | private var mCorrectAnswers: Int = 0
26 |
27 |
28 | private lateinit var mQuestion: TextView
29 | private lateinit var mProgressbar: ProgressBar
30 | private lateinit var mRating: TextView
31 | private lateinit var mOption1: TextView
32 | private lateinit var mOption2: TextView
33 | private lateinit var mOption3: TextView
34 | private lateinit var mOption4: TextView
35 | private lateinit var mSubmitButton: TextView
36 |
37 | //music effects
38 | private lateinit var right: MediaPlayer
39 | private lateinit var wrong: MediaPlayer
40 |
41 |
42 | override fun onCreate(savedInstanceState: Bundle?) {
43 | super.onCreate(savedInstanceState)
44 | setContentView(R.layout.activity_questions2)
45 |
46 | mQuestion = tvQuestion
47 | mProgressbar = progressBar
48 | mRating = rating
49 | mOption1 = option1
50 | mOption2 = option2
51 | mOption3 = option3
52 | mOption4 = option4
53 | mSubmitButton = submitButton
54 |
55 | mUsername = intent.getStringExtra("user").toString()
56 | category = intent.getStringExtra("category").toString()
57 | mQuestionList = GetAllQuestions().fetchData()
58 |
59 | setQuestion()
60 |
61 | mOption1.setOnClickListener(this)
62 | mOption2.setOnClickListener(this)
63 | mOption3.setOnClickListener(this)
64 | mOption4.setOnClickListener(this)
65 | mSubmitButton.setOnClickListener(this)
66 |
67 | //music effects on wrong and right answers
68 | right = MediaPlayer.create(this, R.raw.right)
69 | wrong = MediaPlayer.create(this, R.raw.w)
70 |
71 | }
72 |
73 | @SuppressLint("SetTextI18n")
74 | private fun setQuestion() {
75 |
76 | resetToDefaultOptions()
77 |
78 | val currentQuestion = mQuestionList[mCurrentQuestionIndex - 1]
79 |
80 | if (mCurrentQuestionIndex <= mQuestionList.size) {
81 | mSubmitButton.text = "SUBMIT"
82 | }
83 |
84 | setProgressAnimate(mProgressbar,mCurrentQuestionIndex)
85 | // mProgressbar.progress = mCurrentQuestionIndex
86 | mRating.text = "${mCurrentQuestionIndex}/${mQuestionList.size}"
87 | mQuestion.text = currentQuestion.question
88 | mOption1.text = currentQuestion.option1
89 | mOption2.text = currentQuestion.option2
90 | mOption3.text = currentQuestion.option3
91 | mOption4.text = currentQuestion.option4
92 | }
93 |
94 | private fun resetToDefaultOptions() {
95 | val allOptions = arrayListOf()
96 | allOptions.add(mOption1)
97 | allOptions.add(mOption2)
98 | allOptions.add(mOption3)
99 | allOptions.add(mOption4)
100 | mOption1.background = ContextCompat.getDrawable(this, R.drawable.bg1)
101 | mOption2.background = ContextCompat.getDrawable(this, R.drawable.bg2)
102 | mOption3.background = ContextCompat.getDrawable(this, R.drawable.bg3)
103 | mOption4.background = ContextCompat.getDrawable(this, R.drawable.bg4)
104 |
105 | for (option in allOptions) {
106 | option.setTextColor(Color.parseColor("#FFFFFF"))
107 | option.typeface = Typeface.DEFAULT
108 | }
109 | }
110 |
111 | private fun selectedOptionView(tv: TextView, selectedOptionPosition: Int) {
112 | resetToDefaultOptions()
113 | mSelectOptionPosition = selectedOptionPosition
114 |
115 | tv.setTextColor(Color.parseColor("#363A43"))
116 | tv.setTypeface(tv.typeface, Typeface.BOLD)
117 | tv.background = ContextCompat.getDrawable(
118 | this,
119 | R.drawable.selected_option_clicked_bg
120 | )
121 | }
122 |
123 | @SuppressLint("SetTextI18n")
124 | override fun onClick(view: View) {
125 |
126 | if (view == mOption1) {
127 | selectedOptionView(mOption1, 1)
128 | } else if (view == mOption2) {
129 | selectedOptionView(mOption2, 2)
130 | } else if (view == mOption3) {
131 | selectedOptionView(mOption3, 3)
132 | } else if (view == mOption4) {
133 | selectedOptionView(mOption4, 4)
134 | } else if (view == mSubmitButton) {
135 |
136 | if (mSelectOptionPosition == 0) {
137 | mCurrentQuestionIndex++
138 |
139 | if (mCurrentQuestionIndex <= mQuestionList.size) {
140 | setQuestion()
141 | option1.isEnabled=true
142 | option2.isEnabled=true
143 | option3.isEnabled=true
144 | option4.isEnabled=true
145 | } else {
146 | val intent = Intent(this, ResultActivity::class.java)
147 | intent.putExtra("correct", mCorrectAnswers)
148 | intent.putExtra("user", mUsername)
149 | intent.putExtra("total", 10)
150 | startActivity(intent)
151 | finish()
152 | }
153 |
154 | } else {
155 | val quest = mQuestionList[mCurrentQuestionIndex - 1]
156 | var wrongAns = 0
157 |
158 | if (quest.correct != mSelectOptionPosition) {
159 | wrong.start()
160 | wrongAns = mSelectOptionPosition
161 | option1.isEnabled=false
162 | option2.isEnabled=false
163 | option3.isEnabled=false
164 | option4.isEnabled=false
165 | } else {
166 | right.start()
167 | mCorrectAnswers++
168 | option1.isEnabled=false
169 | option2.isEnabled=false
170 | option3.isEnabled=false
171 | option4.isEnabled=false
172 | }
173 |
174 | for (i in 1..4){
175 | when (i) {
176 | wrongAns -> {
177 | selectedOptionView(i, R.drawable.wrong_option_clicked_bg)
178 | }
179 | quest.correct -> {
180 | selectedOptionView(i, R.drawable.correct_option_clicked_bg)
181 | }
182 | else -> {
183 | selectedOptionView(i,R.drawable.other_options_not_clicked)
184 | }
185 | }
186 | }
187 |
188 | if (mCurrentQuestionIndex == mQuestionList.size) {
189 | mSubmitButton.text = "FINISH"
190 | } else {
191 | mSubmitButton.text = "NEXT"
192 | }
193 |
194 | mSelectOptionPosition = 0
195 | }
196 |
197 |
198 | }
199 | }
200 |
201 | private fun selectedOptionView(answer: Int, drawableView: Int) {
202 |
203 | when (answer) {
204 | 1 -> {
205 | mOption1.background = ContextCompat.getDrawable(
206 | this,
207 | drawableView
208 | )
209 | }
210 | 2 -> {
211 | mOption2.background = ContextCompat.getDrawable(
212 | this,
213 | drawableView
214 | )
215 | }
216 | 3 -> {
217 | mOption3.background = ContextCompat.getDrawable(
218 | this,
219 | drawableView
220 | )
221 | }
222 | 4 -> {
223 | mOption4.background = ContextCompat.getDrawable(
224 | this,
225 | drawableView
226 | )
227 | }
228 | }
229 | }
230 | private fun setProgressAnimate(pb: ProgressBar, progressTo: Int) {
231 | val animation = ObjectAnimator.ofInt(pb, "progress", pb.progress, progressTo * 10)
232 | animation.duration = 500
233 | animation.interpolator = DecelerateInterpolator()
234 | animation.start()
235 | }
236 | }
237 |
238 |
239 |
--------------------------------------------------------------------------------