}
}
}
private fun setQuestion() {
optionsClickableOrNot(true) //set options clickable as new question loaded
isOptionSelected = false //set false as new question loaded
val question = mQuestionsList!![mCurrentPos - 1]
defaultOp()
if (mCurrentPos == 15) {
binding.Submit.text = "Finish"
} else {
binding.Submit.text = "Submit"
}
binding.progressHorizontal.progress = mCurrentPos
binding.textViewProgress.text = "$mCurrentPos " + "/" + "15"
binding.QuestionText.text = question!!.question
binding.Op1.text = question.op1
binding.Op2.text = question.op2
binding.Op3.text = question.op3
binding.Op4.text = question.op4
}
private fun defaultOp() {
val options = ArrayList()
options.add(0, binding.Op1)
options.add(1, binding.Op2)
options.add(2, binding.Op3)
options.add(3, binding.Op4)
for (option in options) {
option.setTextColor(Color.parseColor("#7A8089"))
option.typeface = Typeface.DEFAULT
option.background =
ContextCompat.getDrawable(requireContext(), R.drawable.option_design)
}
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.Op1 -> {
selectedOptionsView(binding.Op1, 1)
isOptionSelected = true
}
R.id.Op2 -> {
selectedOptionsView(binding.Op2, 2)
isOptionSelected = true
}
R.id.Op3 -> {
selectedOptionsView(binding.Op3, 3)
isOptionSelected = true
}
R.id.Op4 -> {
selectedOptionsView(binding.Op4, 4)
isOptionSelected = true
}
R.id.Submit -> {
if (isOptionSelected) {
if (mSelectedop == 0) {
mCurrentPos++
when {
mCurrentPos <= 15 -> {
setQuestion()
}
else -> {
val bundle = bundleOf("correct" to mCorrect, "name" to mUsername)
findNavController().popBackStack()
findNavController().navigate(R.id.resultFragment, bundle)
}
}
} else {
val question = mQuestionsList?.get(mCurrentPos - 1)
if (question!!.correct != mSelectedop) {
answerView(mSelectedop, R.drawable.option_wrong)
} else {
mCorrect++
}
answerView(question.correct, R.drawable.option_correct)
optionsClickableOrNot(false) //set options not-clickable as user has already chosen
if (mCurrentPos == 15) {
binding.Submit.text = "Finish"
} else {
var current = MySharedPreferences.loadData("current")
if (MySharedPreferences.loadData("current") == 100) {
MySharedPreferences.save("current", 100)
} else {
current++
MySharedPreferences.save("current", current)
}
binding.Submit.text = "Next Question"
}
mSelectedop = 0
}
} else {
Toast.makeText(requireContext(), "Please select option", Toast.LENGTH_SHORT)
.show()
}
}
}
}
private fun answerView(answer: Int, drawableview: Int) {
when (answer) {
1 -> {
binding.Op1.background = ContextCompat.getDrawable(requireContext(), drawableview)
}
2 -> {
binding.Op2.background = ContextCompat.getDrawable(requireContext(), drawableview)
}
3 -> {
binding.Op3.background = ContextCompat.getDrawable(requireContext(), drawableview)
}
4 -> {
binding.Op4.background = ContextCompat.getDrawable(requireContext(), drawableview)
}
}
}
private fun selectedOptionsView(tv: TextView, selectedOpNum: Int) {
defaultOp()
mSelectedop = selectedOpNum
tv.setTextColor(Color.parseColor("#808080"))//#363A43
tv.setTypeface(tv.typeface, Typeface.BOLD)
tv.background =
ContextCompat.getDrawable(requireContext(), R.drawable.option_design_selected)
}
private fun optionsClickableOrNot(boolean: Boolean) {
binding.Op1.isEnabled = boolean
binding.Op2.isEnabled = boolean
binding.Op3.isEnabled = boolean
binding.Op4.isEnabled = boolean
}
override fun onResume() {
super.onResume()
(activity as AppCompatActivity?)!!.supportActionBar!!.hide()
}
override fun onStop() {
super.onStop()
(activity as AppCompatActivity?)!!.supportActionBar!!.show()
}
}
package com.example.computergraphics
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.example.computergraphics.databinding.FragmentResultBinding
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [ResultFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class ResultFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
Do'stlaringiz bilan baham: |