Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.9 kB
0
Indexable
Never
package com.example.quizapp

import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_quiz_question.*

class QuizQuestionActivity : AppCompatActivity(),View.OnClickListener {

    private var mCurrentPosition:Int = 1
    private var mQuestionLIst : ArrayList<Question>? = null
    private var selectedOption : Int = 0


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

        mQuestionLIst=Constants.getQuestions()
        SetQuestion()

        OptionOne.setOnClickListener(this)
        OptionTwo.setOnClickListener(this)
        OptionThree.setOnClickListener(this)
        OptionFour.setOnClickListener(this)
        submit.setOnClickListener(this)
    }
    override fun onClick(v: View?) {
        when(v?.id)
        {
            R.id.OptionOne ->{
                SelectedOptionview(OptionOne,1)
            }
            R.id.OptionTwo ->{
                SelectedOptionview(OptionTwo,2)
            }
            R.id.OptionThree ->{
                SelectedOptionview(OptionThree,3)
            }
            R.id.OptionFour ->{
                SelectedOptionview(OptionFour,4)
            }
            R.id.submit ->{
                if (mCurrentPosition==0) {
                    mCurrentPosition++

                    when {
                        mCurrentPosition <= mQuestionLIst!!.size ->{
                            SetQuestion()
                        }
                        else ->{
                            Toast.makeText(
                                this, "You Have Successfully Completed this Quiz...", Toast.LENGTH_SHORT
                            ).show()
                        }

                    }
                }else{
                    val question = mQuestionLIst?.get(mCurrentPosition - 1)
                    if(question!!.CorrectAnswer!=selectedOption)
                    {
                        Answerview(selectedOption,R.drawable.wrong_border)
                    }
                    Answerview(selectedOption,R.drawable.correct_border)

                    if(mCurrentPosition == mQuestionLIst!!.size){
                        submit.text="Finish"
                    }else{
                        submit.text="Go To Next Question"
                    }
                    selectedOption=0;
                }
            }

        }
    }
    private fun SetQuestion()
    {
        val question = mQuestionLIst!!.get(mCurrentPosition-1)
        defaultOptionView()
        if(mCurrentPosition==mQuestionLIst!!.size)
        {
            submit.text="FINISH"
        }
        else
        {
            submit.text="SUBMIT"
        }
        tv_Progress.progress=mCurrentPosition
        tv_textProgress.text = "$mCurrentPosition" + "/" + tv_Progress.max

        tv_appendText1.text=question!!.question
        tv_InputImage.setImageResource(question.image)
        OptionOne.text = question.optionOne
        OptionTwo.text = question.optionTwo
        OptionThree.text = question.optionThree
        OptionFour.text = question.optionFour
    }
    private fun SelectedOptionview(tv:TextView , selectedoptionnum:Int)
    {
        defaultOptionView()
        selectedOption = selectedoptionnum
        tv.setTextColor(Color.parseColor("#363A43"))
        tv.setTypeface(tv.typeface,Typeface.BOLD)
        tv.background = ContextCompat.getDrawable(this,R.drawable.selected_border)
    }
    private fun defaultOptionView()
    {
        val options = ArrayList<TextView>()
        options.add(0,OptionOne)
        options.add(1,OptionTwo)
        options.add(2,OptionThree)
        options.add(3,OptionFour)

        for(option in options)
        {
            option.setTextColor(Color.parseColor("#ACA4A4"))
            option.typeface = Typeface.DEFAULT
            option.background = ContextCompat.getDrawable(this,R.drawable.default_border)
        }
    }


    private fun Answerview(answer:Int ,drawableView :Int )
    {
     when(answer){
         1->{
             OptionOne.background=ContextCompat.getDrawable(this,drawableView)
         }
         2->{
             OptionTwo.background=ContextCompat.getDrawable(this,drawableView)
         }
         3->{
             OptionThree.background=ContextCompat.getDrawable(this,drawableView)
         }
         4->{
             OptionFour.background=ContextCompat.getDrawable(this,drawableView)
         }
     }
    }

}