Posts

[ KOTLIN ] QUIZ APP ANDROID STUDIO || WITH STEP BY STEP PROCESS


Click here to Watch Video on Youtube 



In this article, we will learn how to create a quiz app for android studio kotlin. Follow the below steps to create a quiz app.






 STEP 1:  CREATE A NEW PROJECT WITH EMPTY ACTIVITY


STEP 2: ADD FOLLOWING CODE INTO colors.xml FILE

<color name="white">#FFFFFFFF</color>
<color name="green">#12FA27</color>
<color name="red">#FF1100</color>

STEP 3: ADD FOLLOWING CODE INTO activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7DE7F4"
tools:context=".MainActivity">


<TextSwitcher
android:id="@+id/question"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="@color/white"
android:text="TextView"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextSwitcher
android:id="@+id/option1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="30dp"
android:layout_marginTop="110dp"
android:layout_marginEnd="30dp"
android:background="@color/white"
android:text="TextView"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/question" />

<TextSwitcher
android:id="@+id/option2"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:background="@color/white"
android:text="TextView"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/option1" />

<TextSwitcher
android:id="@+id/option3"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:background="@color/white"
android:text="TextView"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/option2" />

<TextSwitcher
android:id="@+id/option4"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:background="@color/white"
android:text="TextView"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/option3" />

<TextView
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/option4" />

<TextView
android:id="@+id/nextoptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/answer" />


</androidx.constraintlayout.widget.ConstraintLayout>



STEP 4: ADD FOLLOWING CODE INTO MainActivity.kt FILE



import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.CountDownTimer
import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.TextSwitcher
import android.widget.TextView
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat

class MainActivity : Activity() {
@SuppressLint("MissingInflatedId")

val questions = arrayOf("none","Which country has recently been declared by the US as a “non-NATO ally” status?", "What is the dangerous disease caused by bacteria in humans and cattle called?", "In which of the following state, the Sabarimala Sri Dham Shashtha temple dedicated to Lord Ayyappa is located?", "In which of the following year was the budget system introduced for the first time in India?", "On which day is the World Oral Health Day celebrated every year?", "Who of the following is the only Chief of Air Staff to be given the rank of Marshall of the Indian Air Force?", "Which state government has recently launched 'one family, one job' scheme?")
var index = 0
val options = arrayOf("none","China","Qatar","Iraq","Iran","Little Mother","Measles","Anthrax","Malaria","Maharashtra",
"Karnataka","Manipur","Kerala","1867","1860","1897","1890","18 January","20 January","30 July","20 March","Arjan Singh","Subroto Mukherjee","P.C. Lal"," O.P. Mehra","Rajasthan","Madhya Pradesh","Chhattisgarh","Sikkim")
val answers = arrayOf("none","2","3","4","2","4","1","4")



override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

if (Build.VERSION.SDK_INT >= 16) {
getWindow().setFlags(AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT, AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT)
getWindow().getDecorView().setSystemUiVisibility(3328)
}else{
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)

}

val ans = findViewById<TextView>(R.id.answer)
val opt1 = findViewById<TextSwitcher>(R.id.option1)
val opt2 = findViewById<TextSwitcher>(R.id.option2)
val opt3 = findViewById<TextSwitcher>(R.id.option3)
val opt4 = findViewById<TextSwitcher>(R.id.option4)


index = if (index + 1 < questions.size) index + 1 else index + 1

val question = findViewById<TextSwitcher>(R.id.question)
question.setFactory {
val textView = TextView(this@MainActivity)
textView.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
textView.textSize = 20f
textView.setTextColor(Color.BLACK)
textView
}
question.setText(questions[index])
ans.setText(answers[index])

ans.visibility = View.INVISIBLE //keeps size just cant see it.

val nextopt = findViewById<TextView>(R.id.nextoptions)
nextopt.visibility = View.INVISIBLE //keeps size just cant see it.

val timeshow = findViewById<TextView>(R.id.timer)
timeshow.visibility = View.INVISIBLE //keeps size just cant see it.






opt1.setFactory {
val textView = TextView(this@MainActivity)
textView.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
textView.textSize = 20f
textView.setTextColor(Color.BLACK)
textView
}
opt1.setText(options[1])



opt2.setFactory {
val textView = TextView(this@MainActivity)
textView.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
textView.textSize = 20f
textView.setTextColor(Color.BLACK)
textView
}
opt2.setText(options[2])



opt3.setFactory {
val textView = TextView(this@MainActivity)
textView.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
textView.textSize = 20f
textView.setTextColor(Color.BLACK)
textView
}
opt3.setText(options[3])


opt4.setFactory {
val textView = TextView(this@MainActivity)
textView.gravity = Gravity.TOP or Gravity.CENTER_HORIZONTAL
textView.textSize = 20f
textView.setTextColor(Color.BLACK)
textView
}
opt4.setText(options[4])












opt1.setOnClickListener {

val timer = MyCounter(3000, 300)
timer.start()
val ta = ans.text.toString()
if( ta.equals("1") )
{
opt1.setBackgroundColor(getColor(R.color.green))
}
else{
opt1.setBackgroundColor(getColor(R.color.red))
if( ta.equals("2") )
{
opt2.setBackgroundColor(getColor(R.color.green))
}

if( ta.equals("3") )
{
opt3.setBackgroundColor(getColor(R.color.green))
}

if( ta.equals("4") )
{
opt4.setBackgroundColor(getColor(R.color.green))
}

}
}


opt2.setOnClickListener {

val timer = MyCounter(3000, 300)
timer.start()
val ta = ans.text.toString()
if( ta.equals("2") )
{
opt2.setBackgroundColor(getColor(R.color.green))
}
else{
opt2.setBackgroundColor(getColor(R.color.red))
if( ta.equals("1") )
{
opt1.setBackgroundColor(getColor(R.color.green))
}

if( ta.equals("3") )
{
opt3.setBackgroundColor(getColor(R.color.green))
}

if( ta.equals("4") )
{
opt4.setBackgroundColor(getColor(R.color.green))
}

}
}









opt3.setOnClickListener {

val timer = MyCounter(3000, 300)
timer.start()
val ta = ans.text.toString()
if( ta.equals("3") )
{
opt3.setBackgroundColor(getColor(R.color.green))
}
else{
opt3.setBackgroundColor(getColor(R.color.red))
if( ta.equals("1") )
{
opt1.setBackgroundColor(getColor(R.color.green))
}

if( ta.equals("2") )
{
opt2.setBackgroundColor(getColor(R.color.green))
}

if( ta.equals("4") )
{
opt4.setBackgroundColor(getColor(R.color.green))
}

}
}




opt4.setOnClickListener {

val timer = MyCounter(3000, 300)
timer.start()
val ta = ans.text.toString()
if( ta.equals("4") )
{
opt4.setBackgroundColor(getColor(R.color.green))
}
else{
opt4.setBackgroundColor(getColor(R.color.red))
if( ta.equals("1") )
{
opt1.setBackgroundColor(getColor(R.color.green))
}

if( ta.equals("2") )
{
opt2.setBackgroundColor(getColor(R.color.green))
}

if( ta.equals("3") )
{
opt3.setBackgroundColor(getColor(R.color.green))
}

}
}


























}

inner class MyCounter(millisInFuture: Long, countDownInterval: Long) : CountDownTimer(millisInFuture, countDownInterval) {

val question = findViewById<TextSwitcher>(R.id.question)
val ans = findViewById<TextView>(R.id.answer)
val nextopt = findViewById<TextView>(R.id.nextoptions)

val timeshow = findViewById<TextView>(R.id.timer)


override fun onFinish() {
// println("Timer Completed.")

// timeshow.text = "Timer Completed."
index = if (index + 1 < questions.size) index + 1 else index + 1
question.setText(questions[index])
ans.setText(answers[index])
val optseq = nextopt.text.toString()
val optseqi: Int = optseq.toInt()
val optseqnext: Int = optseqi + 4
val optseqnexts = optseqnext.toString()
nextopt.setText(optseqnexts)

val opt1 = findViewById<TextSwitcher>(R.id.option1)
val opt2 = findViewById<TextSwitcher>(R.id.option2)
val opt3 = findViewById<TextSwitcher>(R.id.option3)
val opt4 = findViewById<TextSwitcher>(R.id.option4)

opt1.setText(options[optseqnext + 1])
opt2.setText(options[optseqnext + 2])
opt3.setText(options[optseqnext + 3])
opt4.setText(options[optseqnext + 4])

opt1.setBackgroundColor(getColor(R.color.white))
opt2.setBackgroundColor(getColor(R.color.white))
opt3.setBackgroundColor(getColor(R.color.white))
opt4.setBackgroundColor(getColor(R.color.white))







}

override fun onTick(millisUntilFinished: Long) {
timeshow.textSize = 50f

timeshow.text = (millisUntilFinished / 1000).toString() + ""
println("Timer : " + millisUntilFinished / 1000)
}
}
}



AND DONE. NOW RUN THE APP.
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.